分类: Docker
Docker下面安装MySQL5.7.21

安装MySQL5.7.21镜像

docker pull mysql:5.7.21

查看安装的镜像docker images

[root@cops163 ~]# docker images | grep mysql
docker.io/mysql         5.7.21              5195076672a7        6 days ago          371 MB

创建数据目录

mkdir /data/mysql_data

创建配置文件目录

mkdir /data/conf

创建并运行镜像

docker run -p 3306:3306 --name mysql001 --restart=always -v /data/conf:/etc/my.cnf.d -v /data/mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci

参数说明如下:

-p 3306:3306  #将容器的3306端口映射到主机的3306端口
--name mysql001  #容器的名字为mysql001
--restart=always  #在宿主机重启后或者Docker服务重启后自动启动容器,
no #不重启(不带restart参数时,默认不重启)
on-failure  #退出状态非0时重启
always  #始终重启
-v /data/conf:/etc/my.cnf.d  #将/data/conf目录映射到/etc/my.cnf.d下面 
-v /data/mysql_data:/var/lib/mysql  #将/data/mysql目录映射到/var/lib/mysql下面
-e MYSQL_ROOT_PASSWORD=123456 #设置root初始密码为123456
 -d mysql:5.7.21  #后台运行容器,并返回容器ID
--character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci   #设置编码为utf8mb4

进入Docker MySQL控制台

docker exec -it mysql001 mysql -uroot -p
mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper

Connection id:          2
Current database:       langold_enrolment
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.21 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 47 min 16 sec

Threads: 2  Questions: 1785612  Slow queries: 0  Opens: 246  Flush tables: 1  Open tables: 135  Queries per second avg: 629.623
--------------
mysql> 

查看docker容器的log

docker logs -f mysql001

导入SQL文件到MySQL中

docker exec -i mysql001 mysql -uroot -p123456 langold_enrolment < /tmp/langold_enrolment.sql

导出SQL文件

docker exec -it mysql001 mysqldump -uroot -p123456 aaa > /tmp/aaa.sql

更多参考Docker官方文档:
https://hub.docker.com/r/mysql/mysql-server/


相关博文:

发表新评论