1:安装docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2:查看docker版本
docker -v
Docker version 24.0.7, build afdd53b
3:配置docker镜像加速器
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://yxzrazem.mirror.aliyuncs.com"]
}
4:拉取mysql 5.7镜像
docker pull mysql:5.7
5:创建并运行mysql容器
docker run -d -p 3306:3306 --privileged=true -v /opt/mysql/log:/var/log/mysql -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=oracle --name mysql mysql:5.7
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0a4886c04b8 mysql:5.7 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
6:新建my.cnf
cd /opt/mysql/conf
vi my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
7:重新启动mysql容器实例
docker restart b0a4886c04b8
docker exec -it b0a4886c04b8 /bin/bash
8:连进mysql容器,并建库建表
root@b0a4886c04b8:/# mysql -uroot -poracle
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 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.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)
mysql> use db01;
Database changed
mysql>
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> insert into t1 values(1,'zhangsan');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 1 | 李四 |
+------+----------+
2 rows in set (0.00 sec)
mysql> exit
9:删除mysql容器,重建,并检验上面创建的表的数据是否还在
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0a4886c04b8 mysql:5.7 "docker-entrypoint.s…" 24 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]# docker rm -f b0a4886c04b8
[root@localhost ~]# docker run -d -p 3306:3306 --privileged=true -v /opt/mysql/log:/var/log/mysql -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=oracle --name mysql mysql:5.7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00954681c85e mysql:5.7 "docker-entrypoint.s…" 30 seconds ago Up 30 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]# docker exec -it 00954681c85e /bin/bash
root@00954681c85e:/# mysql -uroot -poracle
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 -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 1 | 李四 |
+------+----------+
2 rows in set (0.00 sec)
mysql> exit
可以看到数据都完好。