返回列表 发帖

CentOS8_在Docker中安装MySQL

笺注:Docker的安装可参考 CentOS8_使用Docker安装Python3


从公网下载镜像:( 以下是下载MySQL5.5的镜像 )
[root@centos8 ~]# docker pull mysql:5.5


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        5.5       d404d78aa797   3 years ago   205MB
[root@centos8 ~]#


使用镜像“mysql:5.5”创建并启动容器“MySQL_1”:
[root@centos8 ~]# docker run -itd --name MySQL_1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=888 -v /opt/MySQL_1:/home/MySQL_1 mysql:5.5
ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个3306是宿主机的端口,暴露给外部直接访问;第二个3306是容器的端口。
-e 环境配置 : 指定数据库管理员root@localhost的密码为888
-v 挂载目录 : 这里是把宿主机的目录/opt/MySQL_1挂载到容器的目录/home/MySQL_1;假如目录不存在,就会自动创建。
mysql:5.5 : 镜像名称:版本号


在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                       NAMES
ff1bb046ba11   mysql:5.5   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   MySQL_1
[root@centos8 ~]#

注释:宿主机的TCP 3306端口,映射到容器“MySQL_1”的TCP 3306端口。



###

进入容器“MySQL_1”:
[root@centos8 ~]# docker exec -it MySQL_1 /bin/bash
root@ff1bb046ba11:/#


查看容器“MySQL_1”的系统版本信息:
root@ff1bb046ba11:/# cat /etc/issue
Debian GNU/Linux 9 \n \l

root@ff1bb046ba11:/# more /etc/debian_version
9.9
root@ff1bb046ba11:/# uname -r
4.18.0-193.el8.x86_64
root@ff1bb046ba11:/#


查看MySQL的版本信息:
root@ff1bb046ba11:/# mysql -V
mysql  Ver 14.14 Distrib 5.5.62, for linux-glibc2.12 (x86_64) using readline 5.1
root@ff1bb046ba11:/#


MySQL的主配置文件:(以下是初始状态)
root@ff1bb046ba11:/# find / -name my.cnf   
/etc/mysql/my.cnf
find: '/proc/1/map_files': Permission denied
root@ff1bb046ba11:/#

root@ff1bb046ba11:/# cat /etc/mysql/my.cnf  
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
!includedir /etc/mysql/conf.d/

root@ff1bb046ba11:/#



本地登录MySQL数据库:(使用的是数据库管理员root@localhost,密码888)
root@ff1bb046ba11:/# mysql -uroot -p888
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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>


查看数据库里有哪些库:(以下3个库是默认就有的)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql>


查看所有数据库用户及其主机信息:(以下是初始状态)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql>



删除数据库用户root@'%':
mysql> drop user root@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)

mysql>



创建一个数据库用户并授权:

授权数据库用户zhuohua@'%'(密码886),可以从任意IP进行访问,对所有的库有完全控制的权限:
mysql> grant all on *.* to zhuohua@'%' identified by '886';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select user,host from mysql.user;                  
+---------+-----------+
| user    | host      |
+---------+-----------+
| zhuohua | %         |
| root    | localhost |
+---------+-----------+
2 rows in set (0.00 sec)

mysql>


查看数据库用户zhuohua@'%'的权限:
mysql> show grants for zhuohua@'%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for zhuohua@%                                                                                            |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhuohua'@'%' IDENTIFIED BY PASSWORD '*F961C54AFEB4D281CE53D7CB8E7822890D86FFFC' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>



###

在宿主机的防火墙firewalld打开TCP 3306端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
3306/tcp
[root@centos8 ~]#



###

Linux客户端远程登录容器“MySQL_1”的MySQL数据库:
[root@localhost ~]# mysql -u"zhuohua" -p"886" -P3306 -h"192.168.168.154"
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.62 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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>

注释:
宿主机的IP地址 192.168.168.154
数据库用户名 zhuohua
密码 886
端口 3306

笺注:
Linux客户端需要安装MySQL或MariaDB
使用数据库用户zhuohua@'%'



在Linux客户端往容器“MySQL_1”的MySQL数据库中创建一个库、一个表:
mysql> create database data1;
Query OK, 1 row affected (0.00 sec)

mysql> use data1;
Database changed
mysql> create table t_1(id int);
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| data1              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use data1;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_data1 |
+-----------------+
| t_1             |
+-----------------+
1 row in set (0.00 sec)

mysql>



######

容器“MySQL_1”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a
[root@centos8 containers]#

[root@centos8 containers]# cd ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a/
[root@centos8 ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a]# ls
checkpoints                                                                hostconfig.json  mounts
config.v2.json                                                             hostname         resolv.conf
ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a-json.log  hosts            resolv.conf.hash
[root@centos8 ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a]#

容器“MySQL_1”的文件config.v2.json中的一些信息:

容器ID : "Config":{"Hostname":"ff1bb046ba11",

端口映射 :
"Ports":{"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"3306"},{"HostIp":"::","HostPort":"3306"}]},

数据库管理员root@localhost的密码 : "Env":["MYSQL_ROOT_PASSWORD=888",

挂载目录的信息:
"MountPoints":{"/home/MySQL_1":{"Source":"/opt/MySQL_1","Destination":"/home/MySQL_1","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/opt/MySQL_1","Target":"/home/MySQL_1"},

















######
######

从公网下载镜像:( 以下是下载MySQL5.6的镜像 )
[root@centos8 ~]# docker pull mysql:5.6


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.6       dd3b2a5dcb48   12 months ago   303MB
mysql        5.5       d404d78aa797   3 years ago     205MB
[root@centos8 ~]#


使用镜像“mysql:5.6”创建并启动容器“MySQL_2”:
[root@centos8 ~]# docker run -itd --name MySQL_2 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=999 mysql:5.6
9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 3307是宿主机的端口,暴露给外部直接访问;3306是容器的端口。
-e 环境配置 : 指定数据库管理员root@localhost的密码为999
mysql:5.6 : 镜像名称:版本号



在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS          PORTS                                       NAMES
9a4622063326   mysql:5.6   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes    0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   MySQL_2
ff1bb046ba11   mysql:5.5   "docker-entrypoint.s…"   5 hours ago     Up 53 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   MySQL_1
[root@centos8 ~]#

注释:宿主机的TCP 3307端口,映射到容器“MySQL_2”的TCP 3306端口。



###

进入容器“MySQL_2”:
[root@centos8 ~]# docker exec -it MySQL_2 /bin/bash
root@9a4622063326:/#


查看容器“MySQL_2”的系统版本信息:
root@9a4622063326:/# cat /etc/issue
Debian GNU/Linux 9 \n \l

root@9a4622063326:/# more /etc/debian_version
9.13
root@9a4622063326:/# uname -r
4.18.0-193.el8.x86_64
root@9a4622063326:/#


查看MySQL的版本信息:
root@9a4622063326:/# mysql -V
mysql  Ver 14.14 Distrib 5.6.51, for Linux (x86_64) using  EditLine wrapper
root@9a4622063326:/#


本地登录MySQL数据库:(使用的是数据库管理员root@localhost,密码999)
root@9a4622063326:/# mysql -uroot -hlocalhost -p999
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 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

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>



查看数据库里有哪些库:(以下3个库是默认就有的)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql>


查看所有数据库用户及其主机信息:(以下是初始状态)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql>



删除数据库用户root@'%':
mysql> drop user root@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)

mysql>



创建一个数据库用户并授权:
MySQL服务器授权数据库用户Python@'192.168.168.130'(密码P@ssw0rd),仅仅在使用IP地址192.168.168.130时可以进行访问,对所有的库有完全控制的权限:
mysql> grant all on *.* to Python@'192.168.168.130' identified by 'P@ssw0rd';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select user,host from mysql.user;  
+--------+-----------------+
| user   | host            |
+--------+-----------------+
| Python | 192.168.168.130 |
| root   | localhost       |
+--------+-----------------+
2 rows in set (0.00 sec)

mysql>


查看数据库用户Python@'192.168.168.130'的权限:
mysql> show grants for Python@'192.168.168.130';
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for Python@192.168.168.130                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'Python'@'192.168.168.130' IDENTIFIED BY PASSWORD '*8232A1298A49F710DBEE0B330C42EEC825D4190A' |
+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>



###

在宿主机的防火墙firewalld打开TCP 3307端口:
firewall-cmd --zone=public --add-port=3307/tcp --permanent
firewall-cmd --reload

查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
3306/tcp 3307/tcp
[root@centos8 ~]#



###

Linux客户端远程登录容器“MySQL_2”的MySQL数据库:
[root@localhost ~]# mysql -u"Python" -p"P@ssw0rd" -P3307 -h"192.168.168.154"
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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>

注释:
宿主机的IP地址 192.168.168.154
数据库用户名 Python
密码 P@ssw0rd
端口 3307

笺注:
Linux客户端需要安装MySQL或MariaDB
Linux客户端的IP地址为 192.168.168.130
使用数据库用户Python@'192.168.168.130'



在Linux客户端往容器“MySQL_2”的MySQL数据库中创建一个库、一个表:
mysql> create database data2;
Query OK, 1 row affected (0.00 sec)

mysql> use data2;
Database changed
mysql> create table t_2(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> use data2;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_data2 |
+-----------------+
| t_2             |
+-----------------+
1 row in set (0.00 sec)

mysql>



######

容器“MySQL_2”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663
ff1bb046ba11af1feb48ad363651c2abeceb7580020979705ead28a2a3264b8a
[root@centos8 containers]#

[root@centos8 containers]# cd 9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663/
[root@centos8 9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663]# ls
9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663-json.log  hostconfig.json  mounts
checkpoints                                                                hostname         resolv.conf
config.v2.json                                                             hosts            resolv.conf.hash
[root@centos8 9a46220633268ca0b3a0fda1117217041fec0c4807f6567eff7beac5cd820663]#

容器“MySQL_2”的文件config.v2.json中的一些信息:

容器ID : "Config":{"Hostname":"9a4622063326",

端口映射 :
"Ports":{"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"3307"},{"HostIp":"::","HostPort":"3307"}]},

数据库管理员root@localhost的密码 : "Env":["MYSQL_ROOT_PASSWORD=999",



###

让容器“MySQL_2”随宿主机的启动而启动:(会自动启动MySQL数据库)
[root@centos8 ~]# docker update --restart=always MySQL_2
MySQL_2
[root@centos8 ~]#


不让容器“MySQL_2”随宿主机的启动而启动:(默认是不会随宿主机的启动而启动)
[root@centos8 ~]# docker update --restart=no MySQL_2
MySQL_2
[root@centos8 ~]#





相关文章:
使用SQLyog远程管理MySQL
Windows2012R2_安装MySQL5.5

CentOS8_在Docker中限制容器可用的CPU个数和内存量
CentOS8_在Docker中安装LAMP

返回列表