Bootstrap

Docker----部署mysql

Docker——Mysql部署

通过docker容器化部署mysql

1.部署mysql

(1)执行命令部署mysql

docker run -v /home/mysql/:/var/lib/mysql \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootadmin \
--restart=always --name mysql -d mysql

执行成功的情况如下

[root@node1 ~]# docker run -v /home/mysql/:/var/lib/mysql \
> -p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootadmin \
> --restart=always --name mysql -d mysql
Unable to find image 'mysql:latest' locally
latest: 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 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d81c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
2e0e6049e41b9d8bc2ba839bca4251ad333613c135cf0eb92a90abeeca5bb6b4

备注:
(1)数据库文件直接挂载到服务器的 /home/mysql/ 目录下
(2)端口号使用默认的3306,root密码是 rootadmin

2.检查执行情况

执行 ls /home/mysql 命令,查看 /home/mysql/ 目录的数据库文件

[root@node1 ~]# ls /home/mysql
auto.cnf       binlog.index  client-cert.pem    #ib_16384_1.dblwr  ib_logfile0  #innodb_temp  performance_schema  server-cert.pem  undo_001
binlog.000001  ca-key.pem    client-key.pem     ib_buffer_pool     ib_logfile1  mysql         private_key.pem     server-key.pem   undo_002
binlog.000002  ca.pem        #ib_16384_0.dblwr  ibdata1            ibtmp1       mysql.ibd     public_key.pem      sys

备注:如上图内容所显示则为挂载成功

3.使用mysql

(1)查找mysql具体docker容器

[root@node1 ~]# docker ps | grep mysql
2e0e6049e41b   mysql  "docker-entrypoint.s…"   12 hours ago   Up 12 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

(2)进入容器内部

[root@node1 ~]# docker exec -it 2e0e6049e41b bash
root@2e0e6049e41b:/#

(3)执行mysql登录和查询命令

root@2e0e6049e41b:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.27 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> 
;