我如何在mysql docker服务上创建一个数据库 [英] how can i create a database on mysql docker service

查看:228
本文介绍了我如何在mysql docker服务上创建一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的gitlab ci服务器上运行moodle phpunit。使用gitlab-ci.yml文件,我创建一个包含php 5.6和mysql服务的容器。

 #Services 
服务:
- mysql:最新

before_script:
- mysql -e'CREATE DATABASE gitlab_ci_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;';

我正在获得 ERROR 2002(HY000):无法连接到本地MySQL服务器通过套接字'/var/run/mysqld/mysqld.sock'(2),不知道如何继续。

解决方案

Tomasz这是我的gitlab-ci.yml文件,你将需要这样的东西:

  #从https://hub.docker.com/r/_/php/ 
图像中选择图像:php:7.0.0

服务:
- mysql:5.7

变量:
#配置mysql环境变量(https://hub.docker.com/r/_/mysql/)
MYSQL_DATABASE:symfony
MYSQL_ROOT_PASSWORD:qwerty

#Composer将所有下载的软件包存储在供应商/目录中。
#如果供应商/目录被提交给
#您的git存储库,请不要使用以下内容。
缓存:
路径:
- 供应商/

before_script:
#安装依赖项
- bash ci / docker_install.sh> / dev / null
- cp ci / parameters.yml app / config / parameters.yml
- composer install

test:app:
script:
- phpunit

这是我的docker_install.sh里面的ci文件夹

 #!/ bin / bash 

#我们需要仅为Docker
安装依赖项[[! -e /.dockerenv]]&& [[! -e /.dockerinit]]&&退出0

set -xe

#安装git(php图像没有),这是composer
apt-get update -yqq
apt-get install git -yqq
apt-get install wget -yqq
apt-get install zip unzip -yqq

#安装composer
curl - sS https://getcomposer.org/installer | php
mv composer.phar / usr / local / bin / composer

#安装phpunit,我们将用于测试的工具
curl -o / usr / local / bin / phpunit https://phar.phpunit.de/phpunit.phar
chmod + x / usr / local / bin / phpunit

#安装mysql驱动
#在这里你可以安装您需要的任何其他扩展名
docker-php-ext-install pdo pdo_mysql mbstring


I'm trying to run moodle phpunit on my gitlab ci server. Using gitlab-ci.yml file i'm creating a container with php 5.6 and mysql service.

# Services
services:
  - mysql:latest

before_script: 
  - mysql -e 'CREATE DATABASE gitlab_ci_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;' ;

I'm getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) and not sure how to proceed.

解决方案

Tomasz this is my gitlab-ci.yml file you will need something like this:

# Select image from https://hub.docker.com/r/_/php/
image: php:7.0.0

services:
  - mysql:5.7

variables:
  # Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
  MYSQL_DATABASE: symfony
  MYSQL_ROOT_PASSWORD: qwerty

# Composer stores all downloaded packages in the vendor/ directory.
# Do not use the following if the vendor/ directory is commited to
# your git repository.
cache:
  paths:
  - vendor/

before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- cp ci/parameters.yml app/config/parameters.yml
- composer install

test:app:
  script:
  - phpunit

And this is my docker_install.sh inside ci folder

#!/bin/bash

# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && [[ ! -e /.dockerinit ]] && exit 0

set -xe

# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
apt-get install wget -yqq
apt-get install zip unzip -yqq

# Install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

# Install phpunit, the tool that we will use for testing
curl -o /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
chmod +x /usr/local/bin/phpunit

# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo pdo_mysql mbstring

这篇关于我如何在mysql docker服务上创建一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆