Docker mount and install MySQL data and configuration files

Download image

# Docker pull the latest version of mysql mirror from the warehouse, if not tagged, the default obtain the latest version Latest
Docker pull mysql

Mounting data volumes and configuration files

# Run mysql named vessel named mysql-service account and set the root password for the initial root 
Docker RUN -d -p 3306: 3306 --name mysql-service -e MYSQL_ROOT_PASSWORD = "root" mysql

Docker into the container

docker exec -it mysql-service bash

Determine within Docker MySQL-related files path

# Find within Docker, the location of the MySQL configuration file my.cnf 
mysql --help | grep my.cnf

Create a local path and mount Docker in the data

Create a local folder

mkdir -p /root/docker/mysql/conf && mkdir -p /root/docker/mysql/data

Copy the test container MySQL configuration file to the path. The future need to change the configuration, can be modified directly in the path of the configuration file on the mount  

docker cp mysql-service:/etc/mysql/my.cnf /root/docker/mysql/conf

Create a container and start the MySQL

docker run --name mysql-server \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=root \
--mount type=bind,src=/root/docker/mysql/conf/my.cnf,dst=/etc/mysql/my.cnf \
--mount type=bind,src=/root/docker/mysql/data,dst=/var/lib/mysql \
--restart=on-failure:3 \
-d mysql
  • --name : specify a name for the container
  • -p : Specifies port mapping format: a host (host) port: the port of the container
  • -e : username = "xxx", set the environment variable
  • --restart = on-failure: 3 cycles restart case refers to a container in the future appear abnormal exit (exit code of non-0) three times:
  • -mount : bind mounts
  • -d : background container and return the container id

  

Changing the character set

If you want to Mysql Chinese garbled does not appear in subsequent operations, you need to modify the configuration files

Log mysql #
mysql -uroot -p
# View command mysql character set 
show variables like '%char%';

 

 

 Modify the configuration file

 Add to my.cnf 

[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8

 Docker mysql-server restart container

docker restart mysql-server

 

Guess you like

Origin www.cnblogs.com/weile0769/p/11863779.html