DEV Community

karenpanahi
karenpanahi

Posted on • Edited on

mysql docker config

https://phoenixnap.com/kb/mysql-docker-container
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55

sudo apt-get install mysql-client

docker run --name=mysqlDocker --env="MYSQL_ROOT_PASSWORD=yourRootPassword" -d mysql/mysql-server 
Enter fullscreen mode Exit fullscreen mode

now you have to change mysql root user host to accept all ips:

docker exec -it <containerName> bash
mysql -u root -p 
# enter the password you entered in the container config

# in mysql bash enter:
update mysql.user set host='%' where user='root';
Enter fullscreen mode Exit fullscreen mode

Configure Mysql Container

you have to use a .cnf file or you will have this error :

Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
Enter fullscreen mode Exit fullscreen mode

create an alternative config file on the host machine and mount them inside the container.

1.First, create a new directory on the host machine:

sudo mkdir -p /root/docker/[container_name]/conf.d
Enter fullscreen mode Exit fullscreen mode

2.Create a custom MySQL config file inside that directory:

sudo nano /root/docker/[container_name]/conf.d/myCustom.cnf
Enter fullscreen mode Exit fullscreen mode

3.Once in the file, you can add lines with the desired configuration.

For example, if you want to increase the maximum number of connections to 250 (instead of the default 151), add the following lines to the configuration file:

[mysqld]
bind-address  = 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

4.change the location of the data directory :
4.1. Find an appropriate volume on the host and create a
data directory on it:

sudo mkdir -p /storage/docker/mysqlData
Enter fullscreen mode Exit fullscreen mode

4.2. Save and exit the file.

  1. For the changes to take place, you need to remove and rerun the MySQL container.
docker run \
--detach \
--name=[container_name] \
--restart unless-stopped
--env="MYSQL_ROOT_PASSWORD=my_password" \
--publish 66030:3306 \
--volume=/root/docker/[container_name]/conf.d:/etc/mysql/conf.d \
--volume=/storage/docker/mysqlData:/var/lib/mysql \
mysql
Enter fullscreen mode Exit fullscreen mode

now you can use either dockerized phpmyadmin or workbench on your host machines.if you want to use phpmyadmin do the following steps otherwise you can jump to step 7.

dockerized phpmyadmin

docker volume create phpmyadminVolume
phpmyadmin-volume

docker run --name mk-phpmyadmin -v phpmyadminVolume:/etc/phpmyadmin/config.user.inc.php --link mk-mysql:db -p 82:80 -d phpmyadmin/phpmyadmin

Enter fullscreen mode Exit fullscreen mode

Access MySQL via phpMyAdmin

Open your browser and visit http://localhost:82 to access phpMyAdmin UI.

Mysql workbench

  1. leave the host option to localhost, in the port section enter the port you exposed for mysql container(in our case 6603) .user and password are the ones you specified in the above section (in our case root ).

bonne chance !

Top comments (0)