1. Run the container, automatically download the image
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
2. Create a host profile mount
mkdir -p ~/i/apps/mysql/{conf,data}
- conf store configuration files
- data store database file
3. The initial configuration file
docker cp mysql:/var/lib/mysql ~/i/apps/mysql/data
- Copy the database to the original file / usr / mysql / data inside
docker cp mysql:/etc/mysql/my.cnf ~/i/apps/mysql/conf/my.cnf
4. Stop delete container
docker rm -f mysql
The boot file generation - start.sh
[root@wanfei ~]# cd ~/i/apps/mysql/ $ cat <<EOF > start.sh #!/bin/bash HOST_NAME=mysql.wanfei.xyz REDIS_DIR=`pwd` docker stop mysql docker rm mysql docker run -d \\ --hostname \${HOST_NAME} \\ -p 3306:3306 \\ --name mysql \\ --privileged=true \\ -v \${REDIS_DIR}/conf/my.cnf:/etc/mysql/my.cnf \\ -v \${REDIS_DIR}/data/mysql:/var/lib/mysql \\ -e MYSQL_ROOT_PASSWORD=123456 \\ mysql EOF
Parameter Description
--hostname \${HOST_NAME}
: Set the access domain address,${HOST_NAME}
is defined abovemysql.wanfei.xyz
this address-p 3306:3306
: The inner container3306
is mapped to the host port of3306
the port--privileged=true
: Container to add specific permissions. Otherwise they willchown: changing ownership of ‘/var/lib/mysql/....‘: Permission denied
-v \${REDIS_DIR}/conf/my.cnf:/etc/mysql/my.cnf
: The host configuredmy.cnf
into this position within the container-v \${REDIS_DIR}/data/mysql:/var/lib/mysql
: The mysql persistent data in the host display, do data backup-e MYSQL_ROOT_PASSWORD=123456
: Specifies the initial database access password, set the first time only a small, rear restart to modify the password prevailmysql
:mysql
Image name, after no tag behind, download the latest tag,latest
6. Run start.sh start mysql
[root@wanfei mysql]# sh start.sh mysql mysql f3d297633f4432495dc587d2860c212a371166897b6a380aceb0ebcdccc5dad8
7. Change password into the container, open the remote connection
# Into the container Docker Exec - IT MySQL / bin / SH # enter MySQL (possible upper invalid password) MySQL - U the root - P # enter 123456, if it fails, the direct Enter MySQL > use MySQL; Reading Table Information for Completion of Table and column names by You CAN Turn OFF the this the Feature to GET A Quicker the Startup with - A Database changed MySQL > the SELECT Host,user,plugin from user; +-----------+------------------+-----------------------+ | host | user | plugin | +-----------+------------------+-----------------------+ | localhost | mysql.infoschema | caching_sha2_password | | localhost | mysql.session | caching_sha2_password | | localhost | mysql.sys | caching_sha2_password | | Localhost | root | caching_sha2_password | + - --------- + ------------------ + ----------- + ----------------------- . 4 rows in sET ( 0.00 sec) # above the root user if localhost, localhost only connections, not remotely connected, is provided below the password given MySQL > the ALTER the uSER ' root ' @ ' % ' the IDENTIFIED the WITH mysql_native_password BY ' xxxxxx ' ; ERROR 1396 (HY000): Operation the ALTER the USER failed for 'root'@'%' #修改localhost为% mysql> update user set host='%' where user='root'; Query OK, 1 row affected (0.14 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select host,user,plugin from user; +-----------+------------------+-----------------------+ | host | user | plugin | +-----------+------------------+-----------------------+ | % | root | caching_sha2_password | | localhost | mysql.infoschema | caching_sha2_password | | localhost | mysql.session | caching_sha2_password | | localhost | mysql.sys | caching_sha2_password | +-----------+------------------+-----------------------+ 4 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxxx'; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)