1. 搜索镜像
docker search mysql
2. 拉取镜像
docker pull mysql:5.7
3. 查看镜像
docker images
4. 创建容器
4.1 创建网络
docker network create mysql-network
docker network ls
4.2 使用 run 命令创建容器
docker run -d \
-p 3306:3306 \
--privileged=true \
-v /itmahy/mysql/log:/var/log/mysql \
-v /itmahy/mysql/data:/var/lib/mysql \
-v /itmahy/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
--name itmahy-mysql \
mysql:5.7
# 查看运行的容器
docker ps
# 查看所有容器
docker ps -a
参数解释:
-p
: 容器中端口3306映射到宿主机的端口为3306--privileged=true
: 容器中的root会得到宿主机的root权限-v
: 容器数据卷,跟容器中的目录一一对应,同步修改、删除 新增文件等操作-e
: 设置容器的环境变量,这里具体设置mysql的root密码--name
: 容器名称-d
: 后台运行
创建容器之后并查看容器是否启动的截图
5. 进入容器登录MySQL
[root@localhost ~]# docker exec -it 158d87a878da /bin/bash
root@158d87a878da:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@158d87a878da:/# 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| itmahy |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
6. Docker中MySQL忘记root密码
在容器的 /etc/mysql/conf.d/目录(根据
docker run -d -p 3306:3306 --privileged=true -v /itmahy/mysql/log:/var/log/mysql -v /itmahy/mysql/data:/var/lib/mysql -v /itmahy/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name itmahy-mysql mysql:5.7
找出对应在服务器上的目录为/itmahy/mysql/conf
,在服务器上编辑比较方便,容器内容没有 vim 命令)新建docker.cnf
文件,并在里面写入:
[mysqld]
skip-grant-tables
重启容器
docker restart itmahy-mysql
进入容器不用密码登录mysql
[root@localhost conf]# docker exec -it 158d87a878da /bin/bash
root@158d87a878da:/# mysql -u root
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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
删除第一步目录下的docker.cnf文件
重启容器,使用再使用刚刚修改的密码登录
7. 修改MySQL默认编码
查看编码
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.01 sec)
修改为 utf8
通过容器卷同步给 mysql 容器实例。
同6,在服务器上的/itmahy/mysql/conf目录下新建 my.cnf文件,写入以下内容:
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
重启容器,并查看编码是否修改成功
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)
docker安装完MySQL并run出容器后,建议请先修改完字符集编码后再新建mysql库-表-插数据