[toc]
root@ubuntu:/tmp/host_data# docker search --limit 3 mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12549 [OK]
mariadb MariaDB Server is a high performing open sou… 4824 [OK]
percona Percona Server is a fork of the MySQL relati… 575 [OK]
docker run mysql:5.7
root@ubuntu:/tmp/host_data# docker run -P -e MYSQL_ROOT_PASSWORD=123456 -d --name mysql01 mysql:5.7
docker: Error response from daemon: Conflict. The container name "/mysql01" is already in use by container "646014e4386df9378362b32465d06baf8ff3dd0c79b32a4017c4d189ab7e3a88". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
root@ubuntu:/tmp/host_data# docker run -P -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
f680877f03bc9786ee219b3a944f23e002dd7c20181e9b067d1a0f3ab706d24f
root@ubuntu:/tmp/host_data# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f680877f03bc mysql:5.7 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:49155->3306/tcp, :::49155->3306/tcp, 0.0.0.0:49154->33060/tcp, :::49154->33060/tcp festive_morse
22e49c33a330 billygoo/tomcat8-jdk8 "catalina.sh run" 10 minutes ago Up 10 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat03
9e194dbf7eca tomcat "catalina.sh run" 24 minutes ago Up 24 minutes 0.0.0.0:49153->8080/tcp, :::49153->8080/tcp tomcat02
c6b2169449f4 ubuntu "bash" 50 minutes ago Up 50 minutes ubuntu2
37d9f24e95a3 ubuntu "bash" 2 hours ago Up 2 hours u1
f19e7fbc3d18 v2fly/v2fly-core "/usr/bin/v2ray -con…" 2 hours ago Restarting (2) 18 seconds ago tcp
有可能自己的服务器mysql被占用3306所以选择随即动态端口
ps -ef|grep mysql
root@ubuntu:/tmp/host_data# docker exec -it f680877f03bc /bin/bash
root@f680877f03bc:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db07 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use db07
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 value(01,"smile");
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | smile |
+------+-------+
1 row in set (0.00 sec)
在主机上同样可以使用工具连接远程的mysql
我们在使用MySQL的时候担心数据被删除怎么办,此时可以用到之前的卷了
避免中文乱码,同步时候要改字符集
docker run -d -p 3306:3306 --privileged=true
-v /mysql/log:/var/log/mysql
-v /mysql/data:/var/lib/mysql
-v /mysql/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=123456
--name mysql_beifen
mysql:5.7
docker run -d -p 3306:3306 --privileged=true -v /mysql/log:/var/log/mysql -v /mysql/data:/var/lib/mysql -v /mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql_beifen mysql:5.7
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)
mysql> use mydata
Database changed
mysql> create table t(
-> id int,
-> name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t values(1,"zhangsan");
Query OK, 1 row affected (0.01 sec)
mysql> select * from t;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
主机上可以看到同步
我们删除MySQL再创建
root@ubuntu:/# docker run -d -p 3306:3306 --privileged=true -v /mysql/log:/var/log/mysql -v /mysql/data:/var/lib/mysql -v /mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql_beifen mysql:5.7
1ff6de0064bdd2eba5acaf0ec5458ca72ae0973f5fec09cf4be2968f159335d3
root@ubuntu:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ff6de0064bd mysql:5.7 "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql_beifen
22e49c33a330 billygoo/tomcat8-jdk8 "catalina.sh run" 47 minutes ago Up 47 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat03
9e194dbf7eca tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:49153->8080/tcp, :::49153->8080/tcp tomcat02
c6b2169449f4 ubuntu "bash" About an hour ago Up About an hour ubuntu2
37d9f24e95a3 ubuntu "bash" 2 hours ago Up 2 hours u1
f19e7fbc3d18 v2fly/v2fly-core "/usr/bin/v2ray -con…" 2 hours ago Restarting (2) 2 seconds ago tcp
root@ubuntu:/# docker exec -it 1ff6de0064bd /bin/bash
root@1ff6de0064bd:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydata |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
由此可见,数据恢复过来了