Docker Community Forums

Share and learn in the Docker community.

Can't create a new database in the mysql container

docker

(Marwenb) #1

Hello evey one ,
I created two containers web conatiner and mysql conatinerand i apply the link between them

docker run --name=web -v mypath:/root/dumps -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydatabase -e MYSQL_USER=user -e MYSQL_PASSWORD=password -d mysql:latest

docker run --name=web --restart=unless-stopped --link mysql:mysql -d -p 80:80 -p 443:443 -v mypath:/var/www/html web

docker exec web env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=f380dc9f00ae
MYSQL_PORT=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_NAME=/web/mysql
MYSQL_ENV_MYSQL_PASSWORD=password
MYSQL_ENV_MYSQL_ROOT_PASSWORD=password
MYSQL_ENV_MYSQL_DATABASE=mydatabase
MYSQL_ENV_MYSQL_USER=user
MYSQL_ENV_GOSU_VERSION=1.7
MYSQL_ENV_MYSQL_MAJOR=5.6
MYSQL_ENV_MYSQL_VERSION=5.6.39-1debian8
PHPIZE_DEPS=autoconf   dpkg-dev   file  g++  gcc  libc-dev   make   pkg-config  re2c
PHP_INI_DIR=/usr/local/etc/php
APACHE_CONFDIR=/etc/apache2
APACHE_ENVVARS=/etc/apache2/envvars
PHP_EXTRA_BUILD_DEPS=apache2-dev
PHP_VERSION=5.6.33
HOME=/root

docker exec -it web bash
root@web:/var/www/html/myproject/doctrine/config/config.yml

This is my config.yml looks like

   connections.demo.dbname: "demo"
   connections.demo.host: mysql
   connections.demo.password: password
   connections.demo.port: null
   connections.demo.user: user

root@web:/var/www/html/myproject bin/console doctrine:database:create

I got this:

 [Doctrine\DBAL\Exception\ConnectionException]
  An exception occured in driver: SQLSTATE[HY000] [1049] Unknown database 'demo'

[Doctrine\DBAL\Driver\PDOException]
 SQLSTATE[HY000] [1049] Unknown database 'demo'

[PDOException]
 SQLSTATE[HY000] [1049] Unknown database 'demo'

Any one can help me to find a solution for this ,
what i want is to create a new database


(Sam) #2

i think you have one typo… the sql container name

also, you started that container with a specific database name…

SQL_DATABASE=mydatabase 

and then try to create another…

and

   connections.demo.port: null

the port cannot be null… as the database is on different ip address than the web container


(Marwenb) #3

Thank you for your answer,
but sorry , i didn’t unserstyand you,
I don’t understand what shoud i do or update in the config or in the commands


(Sam) #4

a container is like a system, it gets its own IP address. so, if the WEB app(system/container) wants to talk to the MYSQL(system/container) , you must use the mysql client syntax that identifies where the server is…

the --link mysql:mysql

means that in the environment of the WEB container is the info on where the MYSQL container is and the ports it exposes…

docker exec web env 

will dump that info out

as for the 1st part…

when you started the MYSQL container, you told it a SPECFIC database to use

-e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydatabase -e MYSQL_USER=user -e MYSQL_PASSWORD=password

then later you tried to create ANOTHER (or so it seems) database from the web container

root@web:/var/www/html/myproject bin/console doctrine:database:create

there is NOTHING different about the application config using containers or multiple systems.


(Marwenb) #5

Ok ,
That’s why i creted a new one without database variable environment

docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6

docker run --name=web --restart=unless-stopped --link mysql2 -d -p 80:80 -p 443:443 -v mypath:/var/www/html web

docker exec web env

HOSTNAME=e41f71604326
MYSQL2_PORT=tcp://172.17.0.3:3306
MYSQL2_PORT_3306_TCP=tcp://172.17.0.3:3306
MYSQL2_PORT_3306_TCP_ADDR=172.17.0.3
MYSQL2_PORT_3306_TCP_PORT=3306
MYSQL2_PORT_3306_TCP_PROTO=tcp
MYSQL2_NAME=/webtv/mysql2
MYSQL2_ENV_MYSQL_ROOT_PASSWORD=password
MYSQL2_ENV_GOSU_VERSION=1.7
MYSQL2_ENV_MYSQL_MAJOR=5.6 
MYSQL2_ENV_MYSQL_VERSION=5.6.39-1debian8
PHPIZE_DEPS=autoconf dpkg-dev  file  g++  gcc  libc-dev   make   pkg-config  re2c
PHP_INI_DIR=/usr/local/etc/php
APACHE_CONFDIR=/etc/apache2
APACHE_ENVVARS=/etc/apache2/envvars
PHP_EXTRA_BUILD_DEPS=apache2-dev
HOME=/root

I’m sure now that the there is a link between the two containers

connections.demo.dbname: "sf_demo"
connections.demo.host: mysql2
connections.demo.password: password
connections.demo.port: 3306
connections.demo.user: root

I put the config as munchned in the env details

root@e41f71604326:/var/www/html/myproject# php bin/console doctrine:database:create

 [Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [1049] Unknown database 'sf_demo'

 [Doctrine\DBAL\Driver\PDOException]
 SQLSTATE[HY000] [1049] Unknown database 'sf_demo'

[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'sf_demo'

(Sam) #6

ok, I would try the mysql client on the web container to make sure you have permissions to access the mysql server database…

by default, mysql prevents users on other systems(web container) from connecting and doing operations…


(Marwenb) #7

How can i make sure that the user have the permission access to the mysql server database,

how can i change the dockerfile or the config or the docker run command to let let user on other systems to connect and doing operation ?


(Sam) #8

see the doc on the mysql image

https://hub.docker.com/r/mysql/mysql-server/


(Marwenb) #9

I nthis case it’s mysql server
so you want me to use mysql server instead of mysql ?


(Sam) #10

if YOU create the image, then YOU decide how to enable the user permissions…
(one of the values of using a supported container)

but this is all standard mysql admin


(Marwenb) #11

docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6

I took this command from docker hub
so what’s the difference , my problem is that i can’t create a new database using php bin/console
and i need to know why , why the system can’t know the new database that’s declared in parametres.yml in my web project

I even add the server_version: 5.6 in the configuration file
And it’s the same result


(Sam) #12

that just starts the container…

Docker Environment Variables

MYSQL_ROOT_HOST: By default, MySQL creates the ‘root’@‘localhost’ account. This account can only be connected to from inside the container. To allow root connections from other hosts, set this environment variable. For example, the value 172.17.0.1, which is the default Docker gateway IP, allows connections from the host machine that runs the container. The option accepts only one entry, but wildcards are allowed (for example, MYSQL_ROOT_HOST=172...* or MYSQL_ROOT_HOST=%).


(Marwenb) #13

so you means that i should add the Environment Variable MYSQL_ROOT_HOST=172.17.0.1


(Sam) #14

i don’t think you are accessing the mysql server as ‘root’…
but u might need to to set access permissions for the real userid…

you should review the mysql admin doc on database permissions…
sounds like you have not used this database technology before


(Marwenb) #15

No i used but not with container concept,
I read many docs , and apply allmost what’s need to be done ,
but its teh same result

But now i exec the mysql container and i create the database sf_demo manually
after that i execute

php bin/console doctrine:schema:update --dump-sql

An exception occurred while executing ‘SELECT w0_.value AS value_0 FROM web_param w0_ WHERE w0_.cle = ‘_host’’:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sf_demo.web_param' doesn't exist

 [Doctrine\DBAL\Driver\PDOException]
 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sf_demo.web_param' doesn't exist

 [PDOException]
 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sf_demo.web_param' doesn't exist

So i think the pb now is not about container but its about an indexing table that is not created


(Marwenb) #16

Thank you for your respponse @sdetweil
Now i can create a database from the web container in the mysql container
http://txt.fliglio.com/2013/11/creating-a-mysql-docker-container/


(Sam) #17

I am glad you got it working!..