docker官网参考链接https://hub.docker.com/_/mysql
[root@gaussdb ~]# docker search mysql --镜像搜索
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation鈥? 13906 [OK]
mariadb MariaDB Server is a high performing open sou鈥? 5298 [OK]
percona Percona Server is a fork of the MySQL relati鈥? 600 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M鈥? 752 [OK]
circleci/mysql MySQL is a widely used, open-source relation鈥? 29
bitnami/mysql Bitnami MySQL Docker Image 80 [OK]
bitnami/mysqld-exporter 4
ubuntu/mysql MySQL open source fast, stable, multi-thread鈥? 43
cimg/mysql 0
rapidfort/mysql RapidFort optimized, hardened image for MySQL 14
google/mysql MySQL server for Google Compute Engine 23 [OK]
ibmcom/mysql-s390x Docker image for mysql-s390x 2
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ鈥? 0
hashicorp/mysql-portworx-demo 0
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa鈥? 1 [OK]
rapidfort/mysql-official RapidFort optimized, hardened image for MySQ鈥? 0
databack/mysql-backup Back up mysql databases to... anywhere! 82
linuxserver/mysql A Mysql container, brought to you by LinuxSe鈥? 38
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d鈥? 0
vitess/mysqlctld vitess/mysqlctld 1 [OK]
linuxserver/mysql-workbench 48
eclipse/mysql Mysql 5.7, curl, rsync 0 [OK]
bitnamicharts/mysql 0
drud/mysql 0 [root@gaussdb ~]# docker pull mysql:5.7 --拉取镜像
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7[root@gaussdb ~]# docker images mysql:5.7 --查看拉取的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 14 months ago 448MB
[root@gaussdb ~]# ps -ef|grep 3306 --创建mysql容器之前查看是否有3306端口占用
root 18085 17645 0 14:37 pts/1 00:00:00 grep --color=auto 3306**创建mysql容器语法:**
$ docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7**开始创建MySQL容器:**
[root@gaussdb ~]# docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
fa5e1bf902e9573039d3160850dd362582e40c993a1d7fd19127c3721fc4a6eb[root@gaussdb ~]# docker ps --查看创建的MySQL容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa5e1bf902e9 mysql:5.7 "docker-entrypoint.s鈥? 14 seconds ago Up 12 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@gaussdb ~]# docker exec -it fa5e1bf902e9 /bin/bash --进入MySQL容器
root@fa5e1bf902e9:/# mysql -uroot -p123456 --登录MySQL
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 |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
配置容器数据卷,这样容器中的数据就会保留在宿主机上,即使删除容器数据也不会丢失。
用容器数据卷的方式创建MySQL容器:
**命令格式:**
docker run -d -p 3306:3306 --privileged=true -v /zy/mysql/log:/var/log/mysql -v /zy/mysql/data:/var/lib/mysql -v /zy/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
**创建容器数据卷形式的MySQL容器:**--宿主机上的文件路径要提前创建好。
[root@gaussdb conf]# docker run -d -p 3306:3306 --privileged=true -v /zy/mysql/log:/var/log/mysql -v /zy/mysql/data:/var/lib/mysql -v /zy/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
**创建完成:**
[root@gaussdb conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c44f7fb5d185 mysql:5.7 "docker-entrypoint.s鈥? 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
测试删除容器数据是否会丢失:
**登录容器数据库并插入数据:**
root@c44f7fb5d185:/# mysql -u root -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 3
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> create database db01;
Query OK, 1 row affected (0.00 sec)mysql> use db01;
Database changedmysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.03 sec)mysql> insert into t1 values(1,z3);
ERROR 1054 (42S22): Unknown column 'z3' in 'field list'
mysql> insert into t1 values(1,'z3');
Query OK, 1 row affected (0.04 sec)mysql> exit
Bye
root@c44f7fb5d185:/# exit
exit
**删除MySQL容器:**
[root@gaussdb conf]# docker rm -f mysql
mysql
[root@gaussdb conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES**查看宿主机上的数据文件是否还在:**
[root@gaussdb conf]# cd /zy/mysql/data/
这边可以看到容器删掉了但是db01还在
[root@gaussdb data]# ls
auto.cnf ca.pem client-key.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
ca-key.pem client-cert.pem db01 ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
验证数据库中数据是否还在:
**重新创建MySQL容器:**
[root@gaussdb data]# docker run -d -p 3306:3306 --privileged=true -v /zy/mysql/log:/var/log/mysql -v /zy/mysql/data:/var/lib/mysql -v /zy/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
6eafb769d5946ce419036315c50ebc247c4e70c81118506a977b94f21d485cc5
**登录容器数据库,并验证数据:**
[root@gaussdb data]# docker exec -it mysql /bin/bash
root@6eafb769d594:/# 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 |
| db01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)mysql> use db01;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | z3 |
+------+------+
1 row in set (0.00 sec)
数据正常。
字符集问题处理:
docker中创建的MySQL实例,默认是latin1字符集,如果插入中文会报错,需要修改字符集。
**未修改字符集前,字符集都是latin1**
mysql> show VARIABLES like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)**修改容器数据库配置文件,因为之前用了容器数据卷,所以可以在宿主机上直接修改或者创建my.conf配置文件。**
[root@gaussdb mysql]# cd /zy/mysql/conf/
[root@gaussdb conf]# vi my.cnf
[root@gaussdb conf]# cat my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8**重启mysql容器**
[root@gaussdb conf]# docker restart mysql
mysqlroot@c44f7fb5d185:/# mysql -u root -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> show VARIABLES like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
字符集已经修改完成,可插入汉字验证。