背景
我正在尝试使用http://wercker.com为 Yii2 应用程序构建一些自动化测试。我创建了自己的 Docker 存储库https://hub.docker.com/r/consynki/yii2/,它提供了一个简单的 LAMP 堆栈。
我将 Docker 存储库用作 wercker.yml 文件中的框,通过几个简单的步骤来设置我的应用程序,初始化数据库,然后运行我的 phpunit 测试
box: consynki/yii2
build:
steps:
- script:
name: Update enviroment dependencies
code: |-
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
- script:
name: Install Composer dependencies
code: |-
rm -rf ./vendor
/usr/bin/composer install --no-interaction --prefer-source
- script:
name: Apache site install
code: |-
sudo chmod -R 755 /var/www
sudo cp -r ./ /var/www/example.lan/
sudo chown -R $USER:$USER /var/www/example.lan/
sudo cp ./config/example.lan.conf /etc/apache2/sites-available/example.lan.conf
sudo a2ensite example.lan.conf
sudo cp -fr ./config/hosts /etc/hosts
sudo service apache2 restart
- script:
name: Create database
code: |-
mysql -uroot -e "SHOW DATABASES;"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS test"
- script:
name: PHPUnit integration tests
code: |-
./vendor/bin/phpunit --configuration phpunit.xml
问题
问题是,当我使用 wercker CLI 运行构建时wercker build
,它无法连接到 mysql。ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
尝试运行“创建数据库”步骤时,我不断收到以下连接错误。
- script:
name: Create database
code: |-
mysql -uroot -e "SHOW DATABASES;"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS test"
我知道 docker 容器有一个 mysql 连接,因为我可以 ssh 进入它并运行mysql -uroot -e "SHOW DATABASES;"
编辑我当前正在进行的工作 wercker 灯模板可在https://github.com/levi-putna/php-cli-template获得
要求
我知道 wercker.com 相对较新,并且没有很多文档。但我希望有人可以为我提供一个如何在 wercker 测试运行中使用 mysql 的示例。
编辑 - 使用 Wercker 服务
我也尝试使用 Wercker MariaBD 服务进行连接,它不完全是 MySQL,所以不是对我的生产环境的真正测试,但应该足以开始测试。
我更新了 wercker.yml 以添加 mariadb 服务。简化了我让数据库连接工作正常的步骤。
box: consynki/yii2
services:
- id: mariadb
# your credentials for Docker Hub
username: $USERNAME
password: $PASSWORD
tag: latest
# set the required environment variable
env:
MYSQL_ROOT_PASSWORD: mypassword
MYSQL_DATABASE: test_database
MYSQL_USER: admin
MYSQL_PASSWORD: test123
build:
steps:
- script:
name: Create database
code: |-
mysql -h $MARIADB_PORT_3306_TCP_ADDR -P $MARIADB_PORT_3306_TCP_PORT -u $MYSQL_USER -p test123 -v
mysql -h $MARIADB_PORT_3306_TCP_ADDR -P $MARIADB_PORT_3306_TCP_PORT -u $MYSQL_USER -p test123 -e "SHOW DATABASES;"
我似乎仍然遇到与我原来的方法相同的错误。ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
在 wercker 步骤退出后,我还看到了一些额外的日志消息。
WARNING Box container has already stopped.
Initializing database
2016-03-01 22:51:00 140429748197312 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 51 ...
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Database physically writes the file full: wait...
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-03-01 22:51:01 140429748197312 [Warning] InnoDB: New log files created, LSN=45883
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Doublewrite buffer created
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:01 140429748197312 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Foreign key constraint system tables created
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Tablespace and datafile system tables created.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 0
2016-03-01 22:51:01 140428963710720 [Note] InnoDB: Dumping buffer pool(s) not yet started
2016-03-01 22:51:03 140216194230208 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 80 ...
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Highest supported file format is Barracuda.
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 1616799
2016-03-01 22:51:04 140215410722560 [Note] InnoDB: Dumping buffer pool(s) not yet started
2016-03-01 22:51:06 140659046377408 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 109 ...
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Highest supported file format is Barracuda.
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 1616809
2016-03-01 22:51:06 140658262116096 [Note] InnoDB: Dumping buffer pool(s) not yet started
ERROR: 1049 Unknown database 'test'
2016-03-01 22:51:06 140659046377408 [ERROR] Aborting