Create a build directory structure
# MKDIR-PV docker/mysql# CD docker/mysql/
Two write dockerfile files
# vim dockerfile #此处 sshd:latest Image created for the previous article # this dockerfile " dookerpool"'s dockerfile file, just made some optimizations and changes during testing From sshd:latestmaintainer mykernel (www.mykernel.cn) # Install software RUN ENV DEBIAN_FRONTEND noninteractive apt-get update && \apt-get -yq install mysql-server-5.6 pwgen && \rm -rf /var/ lib/apt/lists/*# Delete pre-installed database files run rm -rf /var/lib/mysql/*# Add folders under MYSQL Configuration file Add my.cnf /etc/mysql/conf.d/my.cnfadd mysqld_charset.cnf /etc/mysql/conf.d/mysqld_ charset.cnfadd authorized_keys /root/.ssh/authorized_keys# Add MYSQL Script add import_sql.sh /import_sql.shadd run.sh /run.shrun chmod 755 /*.sh# Setting environment variables, User name and password env mysql_user adminenv mysql_pass **random**# setting environment variables in master-slave copy mode env Replication_master **false**env replication_slave **false**env replication_user replicaenv replication_pass replica# settings can allow mounted volumes that can be used to back up databases and configuration files volume ["/etc/mysql", "/var/lib/mysql"]# settings can be mapped to the port if it is from our sshd image inheritance, the default will also open 22 port expose 3306cmd ["/run.sh"]
Three prepare files for the add that are required in Dockefile
# Touch my.cnf mysqld_charset.cnf authorized_keys import_sql.sh run.sh
(1) MY.CNF is the main configuration file of MySQL runtime, it needs to write according to its actual situation.
(2) mysqld_charset.cnf for MySQL runtime character configuration, need to write according to their actual situation
(3) import_sql.sh
(4) Run.sh This provides a run file with master and slave copy and custom password
Four Create mirrors
# docker Build-t Mysql:first.
Five Test
# sudo docker run -d -p 1027:22 -p 1026:3306 --name mysql mysql:first
Connect to the container via Xshell to see if the server is started, use the MySQL command directly here, then enter as root, configure the relevant user and then use the close container, Docker commit to commit.
Remote connection mysqld test via MySQL Default user name and user password, the default user for admin password can be viewed using Docker logs MySQL. The Admin user has all the root permissions.
Note: Each time you start the container with the-p option, the port will change after each stop container and start, so we recommend using-p to specify a fixed port. Reduces the ability to modify iptables rules or cloud server security policies
Six-mount MySQL data volume
# docker run -d -p -v /mydata/mysql:/var/ Lib/mysql mysql:first
In fact, MySQL's Dockerfile file already uses the volume directive to create a valid space for the container (not deleted by deleting the container), but the space is newly created, and the commands on the physical machine are more complex. Here, you can use the-v option to mount a host's current directory directly, and the container can access all the data in that directory directly.
Seven set MySQL Default user's password
The default MySQL password is used after launching the container using Docker logs | view. To modify the password for the default User (Admin):
# docker run-d-p-e mysql_pass= "Your_pass" Mysql:first
The-e parameter can be used to set any environment variable within the container. Even overwrite environment variables that already exist, or environment variables that are set through env in Dockerfile.
Eight enable master-slave mode
Create a MySQL main container
# Docker run-d-e replication_master=true-p-name Mysql:first
Create MySQL from container (–link name:alias)
# Docker run-d-e replication_slave=true--link mysql:mysql Mysql:first
Note: The –name of the main container must be MySQL, otherwise it will be an error.
Docker builds mysqld service image based on Dockerfile