笺注:Docker的安装可参考 CentOS8_使用Docker安装Python3
[root@centos8 ~]# dnf -y install net-tools
[root@centos8 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:25:b7:b9:07 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 56932 bytes 83375047 (79.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14676 bytes 888943 (868.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
给Docker创建自定义虚拟网卡,并且指定网段:
[root@centos8 ~]# docker network create --subnet=172.18.0.0/16 staticnet
2be66df3b3958685e0ec2a62098577ad95590c83d7f3f4c4e967fc9b5626508c
[root@centos8 ~]#
备注:给Docker创建自定义虚拟网卡后,容器才能使用固定IP地址;
查看Docker所有的虚拟网卡:
[root@centos8 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
0527decaf262 bridge bridge local
ecf4d5fc9a71 host host local
f3a83278f2d8 none null local
2be66df3b395 staticnet bridge local
[root@centos8 ~]#
备注:
网卡ID“2be66df3b395”是自动生成的;
网卡名“staticnet”是自定义的;
[root@centos8 ~]# ifconfig
br-2be66df3b395: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:39:8b:2d:20 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:25:b7:b9:07 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 56961 bytes 83377119 (79.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14689 bytes 891361 (870.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
###
从公网下载镜像:( 以下是下载MariaDB10.1的镜像 )
[root@centos8 ~]# docker pull mariadb:10.1
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.1 895244a22f37 21 months ago 352MB
[root@centos8 ~]#
使用镜像“mariadb:10.1”创建并启动容器“MariaDB_1”:
[root@centos8 ~]# docker run -itd --name MariaDB_1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=888 -v /opt/database_1:/home/database_1 --net staticnet --ip 172.18.0.2 mariadb:10.1
2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个3306是宿主机的端口,暴露给外部直接访问;第二个3306是容器的端口。
-e 环境配置 : 指定数据库管理员root@localhost的密码为888
-v 挂载目录 : 这里是把宿主机的目录/opt/database_1挂载到容器的目录/home/database_1;假如目录不存在,就会自动创建。
--net staticnet --ip : 指定容器的固定IP地址(使用虚拟网卡“staticnet”)。
mariadb:10.1 : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fb5f70092ae mariadb:10.1 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp MariaDB_1
[root@centos8 ~]#
注释:宿主机的TCP 3306端口,映射到容器“MariaDB_1”的TCP 3306端口。
###
进入容器“MariaDB_1”:
[root@centos8 ~]# docker exec -it MariaDB_1 /bin/bash
root@2fb5f70092ae:/#
查看容器“MariaDB_1”的系统版本信息:
root@2fb5f70092ae:/# cat /etc/issue
Ubuntu 18.04.5 LTS \n \l
root@2fb5f70092ae:/# uname -r
4.18.0-193.el8.x86_64
root@2fb5f70092ae:/#
查看容器“MariaDB_1”的IP地址:
root@2fb5f70092ae:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
5: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@2fb5f70092ae:/#
备注:
宿主机或容器重启后,IP地址都是不变的;
正常的话,容器“MariaDB_1”是可以Ping通宿主机:
root@2fb5f70092ae:/# ping 172.18.0.1
bash: ping: command not found
root@2fb5f70092ae:/#
root@2fb5f70092ae:/# apt-get update
root@2fb5f70092ae:/# apt-get -y install inetutils-ping
root@2fb5f70092ae:/# ping -c 4 172.18.0.1
PING 172.18.0.1 (172.18.0.1): 56 data bytes
64 bytes from 172.18.0.1: icmp_seq=0 ttl=64 time=0.093 ms
64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.081 ms
64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.090 ms
64 bytes from 172.18.0.1: icmp_seq=3 ttl=64 time=0.102 ms
--- 172.18.0.1 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.081/0.091/0.102/0.000 ms
root@2fb5f70092ae:/#
root@2fb5f70092ae:/# ping -c 4 192.168.168.154
PING 192.168.168.154 (192.168.168.154): 56 data bytes
64 bytes from 192.168.168.154: icmp_seq=0 ttl=64 time=0.141 ms
64 bytes from 192.168.168.154: icmp_seq=1 ttl=64 time=0.087 ms
64 bytes from 192.168.168.154: icmp_seq=2 ttl=64 time=0.097 ms
64 bytes from 192.168.168.154: icmp_seq=3 ttl=64 time=0.077 ms
--- 192.168.168.154 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.077/0.100/0.141/0.024 ms
root@2fb5f70092ae:/#
正常的话,容器“MariaDB_1”是可以访问公网:
root@2fb5f70092ae:/# ping -c 5 blog.zhuohua.store
PING blog.zhuohua.store (47.75.39.177): 56 data bytes
64 bytes from 47.75.39.177: icmp_seq=0 ttl=127 time=17.561 ms
64 bytes from 47.75.39.177: icmp_seq=1 ttl=127 time=20.544 ms
64 bytes from 47.75.39.177: icmp_seq=2 ttl=127 time=22.424 ms
64 bytes from 47.75.39.177: icmp_seq=3 ttl=127 time=17.815 ms
64 bytes from 47.75.39.177: icmp_seq=4 ttl=127 time=19.912 ms
--- blog.zhuohua.store ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 17.561/19.651/22.424/1.805 ms
root@2fb5f70092ae:/#
###
查看MariaDB的版本信息:
root@2fb5f70092ae:/# mysql -V
mysql Ver 15.1 Distrib 10.1.48-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
root@2fb5f70092ae:/#
MariaDB的主配置文件:(以下是初始状态)
root@2fb5f70092ae:/# find / -name my.cnf
/etc/mysql/my.cnf
find: '/proc/1/map_files': Permission denied
root@2fb5f70092ae:/#
root@2fb5f70092ae:/# cat /etc/mysql/my.cnf |grep -v "^#" |grep -v "^$"
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
skip-external-locking
max_connections = 100
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
thread_cache_size = 128
sort_buffer_size = 4M
bulk_insert_buffer_size = 16M
tmp_table_size = 32M
max_heap_table_size = 32M
myisam_recover_options = BACKUP
key_buffer_size = 128M
table_open_cache = 400
myisam_sort_buffer_size = 512M
concurrent_insert = 2
read_buffer_size = 2M
read_rnd_buffer_size = 1M
query_cache_limit = 128K
query_cache_size = 64M
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
expire_logs_days = 10
max_binlog_size = 100M
default_storage_engine = InnoDB
innodb_buffer_pool_size = 256M
innodb_log_buffer_size = 8M
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 400
innodb_flush_method = O_DIRECT
[galera]
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M
!includedir /etc/mysql/conf.d/
root@2fb5f70092ae:/#
本地登录MariaDB数据库:(使用的是数据库管理员root@localhost,密码888)
root@2fb5f70092ae:/# mysql -uroot -p888
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.48-MariaDB-1~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
查看数据库里有哪些库:(以下3个库是默认就有的)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
MariaDB [(none)]>
查看所有数据库用户及其主机信息:(以下是初始状态)
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]>
删除数据库用户root@'%':
MariaDB [(none)]> drop user root@'%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]>
创建一个数据库用户并授权:
授权数据库用户zhuohua@'%'(密码886),可以从任意IP进行访问,对所有的库有完全控制的权限:
MariaDB [(none)]> grant all on *.* to zhuohua@'%' identified by '886';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| zhuohua | % |
| root | localhost |
+---------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]>
查看数据库用户zhuohua@'%'的权限:
MariaDB [(none)]> show grants for zhuohua@'%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for zhuohua@% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhuohua'@'%' IDENTIFIED BY PASSWORD '*F961C54AFEB4D281CE53D7CB8E7822890D86FFFC' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
###
宿主机远程登录容器“MariaDB_1”的MariaDB数据库:
[root@centos8 ~]# mysql -u"zhuohua" -p"886" -P3306 -h"172.18.0.2"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.1.48-MariaDB-1~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
注释:
容器“MariaDB_1”的IP地址 172.18.0.2
数据库用户名 zhuohua
密码 886
端口 3306
笺注:
宿主机安装了MariaDB
使用数据库用户zhuohua@'%'
在宿主机往容器“MariaDB_1”的MariaDB数据库中创建一个库、一个表:
MariaDB [(none)]> create database data1;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use data1;
Database changed
MariaDB [data1]> create table t_1(id int);
Query OK, 0 rows affected (0.008 sec)
MariaDB [data1]>
MariaDB [data1]> show databases;
+--------------------+
| Database |
+--------------------+
| data1 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [data1]>
在MariaDB下查看当前使用的是哪个库:
MariaDB [data1]> Select database();
+------------+
| database() |
+------------+
| data1 |
+------------+
1 row in set (0.000 sec)
MariaDB [data1]>
MariaDB [data1]> Show tables;
+-----------------+
| Tables_in_data1 |
+-----------------+
| t_1 |
+-----------------+
1 row in set (0.000 sec)
MariaDB [data1]>
###
在宿主机的防火墙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客户端远程登录容器“MariaDB_1”的MariaDB数据库:
[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 4
Server version: 5.5.5-10.1.48-MariaDB-1~bionic mariadb.org binary distribution
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
使用数据库用户zhuohua@'%'
在Linux客户端往容器“MariaDB_1”的MariaDB数据库的库data1中的表t_1中插入记录:
mysql> Show databases;
+--------------------+
| Database |
+--------------------+
| data1 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use data1;
Database changed
mysql> Select database();
+------------+
| database() |
+------------+
| data1 |
+------------+
1 row in set (0.00 sec)
mysql> Show tables;
+-----------------+
| Tables_in_data1 |
+-----------------+
| t_1 |
+-----------------+
1 row in set (0.00 sec)
mysql>
mysql> INSERT INTO t_1 (id) VALUES (1);
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO t_1 (id) VALUES (2);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> Select * from t_1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql>
######
容器“MariaDB_1”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a
[root@centos8 containers]#
[root@centos8 containers]# cd 2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a/
[root@centos8 2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a]# ls
2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a-json.log hostconfig.json mounts
checkpoints hostname resolv.conf
config.v2.json hosts resolv.conf.hash
[root@centos8 2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a]#
容器“MariaDB_1”的文件config.v2.json中的一些信息:
容器ID : "Config":{"Hostname":"2fb5f70092ae",
IP地址 : (使用虚拟网卡“staticnet”)
"Networks":{"staticnet":{"IPAMConfig":{"IPv4Address":"172.18.0.2"},"Links":null,"Aliases":["2fb5f70092ae"],"NetworkID":"2be66df3b3958685e0ec2a62098577ad95590c83d7f3f4c4e967fc9b5626508c","EndpointID":"c7a7af3d1f187920f0e0a8bc18f90f8530cea73474381adeada245144e7f68de","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,
端口映射 :
"Ports":{"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"3306"},{"HostIp":"::","HostPort":"3306"}]},
数据库管理员root@localhost的密码 : "Env":["MYSQL_ROOT_PASSWORD=888",
挂载目录的信息 :
"MountPoints":{"/home/database_1":{"Source":"/opt/database_1","Destination":"/home/database_1","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/opt/database_1","Target":"/home/database_1"},
######
######
使用镜像“mariadb:10.1”创建并启动容器“MariaDB_2”:
[root@centos8 ~]# docker run -itd --name MariaDB_2 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=P@ssw0rd -v /opt/database_2:/home/database_2 --net staticnet --ip 172.18.0.3 mariadb:10.1
7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 3307是宿主机的端口,暴露给外部直接访问;3306是容器的端口。
-e 环境配置 : 指定数据库管理员root@localhost的密码为P@ssw0rd
-v 挂载目录 : 这里是把宿主机的目录/opt/database_2挂载到容器的目录/home/database_2;假如目录不存在,就会自动创建。
--net staticnet --ip : 指定容器的固定IP地址(使用虚拟网卡“staticnet”)。
mariadb:10.1 : 镜像名称:版本号
在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7854e064cb29 mariadb:10.1 "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp MariaDB_2
2fb5f70092ae mariadb:10.1 "docker-entrypoint.s…" 4 hours ago Up 10 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp MariaDB_1
[root@centos8 ~]#
注释:宿主机的TCP 3307端口,映射到容器“MariaDB_2”的TCP 3306端口。
###
进入容器“MariaDB_2”:
[root@centos8 ~]# docker exec -it MariaDB_2 /bin/bash
root@7854e064cb29:/#
查看容器“MariaDB_2”的IP地址:
root@7854e064cb29:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
33: eth0@if34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@7854e064cb29:/#
正常的话,容器“MariaDB_2”是可以Ping通宿主机:
root@7854e064cb29:/# apt-get update
root@7854e064cb29:/# apt-get -y install inetutils-ping
root@7854e064cb29:/# ping -c 4 172.18.0.1
PING 172.18.0.1 (172.18.0.1): 56 data bytes
64 bytes from 172.18.0.1: icmp_seq=0 ttl=64 time=0.104 ms
64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.081 ms
64 bytes from 172.18.0.1: icmp_seq=3 ttl=64 time=0.082 ms
--- 172.18.0.1 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.079/0.087/0.104/0.000 ms
root@7854e064cb29:/#
正常的话,容器“MariaDB_2”是可以Ping通宿容器“MariaDB_1”:
root@7854e064cb29:/# ping -c 4 172.18.0.2
PING 172.18.0.2 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.160 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.105 ms
64 bytes from 172.18.0.2: icmp_seq=3 ttl=64 time=0.087 ms
--- 172.18.0.2 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.087/0.110/0.160/0.030 ms
root@7854e064cb29:/#
本地登录MariaDB数据库:(使用的是数据库管理员root@localhost,密码P@ssw0rd)
root@7854e064cb29:/# mysql -uroot -hlocalhost -pP@ssw0rd
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.48-MariaDB-1~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
容器“MariaDB_2”远程登录容器“MariaDB_1”的MariaDB数据库:
root@7854e064cb29:/# mysql -u"zhuohua" -p"886" -P3306 -h"172.18.0.2"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.48-MariaDB-1~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| data1 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>
注释:
容器“MariaDB_1”的IP地址 172.18.0.2
数据库用户名 zhuohua
密码 886
端口 3306
笺注:
使用数据库用户zhuohua@'%'
###
在容器“MariaDB_2”的MariaDB数据库中,创建一个数据库用户并授权:
授权数据库用户Python@'%'(密码P@ssw0),可以从任意IP进行访问,对所有的库有完全控制的权限:
MariaDB [(none)]> grant all on *.* to Python@'%' identified by 'P@ssw0';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> Select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| Python | % |
| root | % |
| root | localhost |
+--------+-----------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
查看数据库用户Python@'%'的权限:
MariaDB [(none)]> Show grants for Python@'%';
+----------------------------------------------------------------------------------------------------------------+
| Grants for Python@% |
+----------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'Python'@'%' IDENTIFIED BY PASSWORD '*F98118BBC8B06F8493726F8CDD0FB5D10DD8B16E' |
+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
###
宿主机远程登录容器“MariaDB_2”的MariaDB数据库:
[root@centos8 ~]# mysql -u"Python" -p"P@ssw0" -P3306 -h"172.18.0.3"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.48-MariaDB-1~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> Select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| Python | % |
| root | % |
| root | localhost |
+--------+-----------+
3 rows in set (0.000 sec)
MariaDB [(none)]>
注释:
容器“MariaDB_2”的IP地址 172.18.0.3
数据库用户名 Python
密码 P@ssw0
端口 3306
笺注:
宿主机安装了MariaDB
使用数据库用户Python@'%'
###
在宿主机的防火墙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客户端远程登录容器“MariaDB_2”的MariaDB数据库:
[root@localhost ~]# mysql -u"Python" -p"P@ssw0" -P3307 -h"192.168.168.154"
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.5-10.1.48-MariaDB-1~bionic mariadb.org binary distribution
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>
mysql> Select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| Python | % |
| root | % |
| root | localhost |
+--------+-----------+
3 rows in set (0.00 sec)
mysql>
注释:
宿主机的IP地址 192.168.168.154
数据库用户名 Python
密码 P@ssw0
端口 3307
笺注:
Linux客户端安装了MySQL
使用数据库用户Python@'%'
######
容器“MariaDB_2”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
2fb5f70092ae83d1f533ff5b7ae664867462f82c03b69a5dbf9f73f9b77c211a
7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc
[root@centos8 containers]#
[root@centos8 containers]# cd 7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc/
[root@centos8 7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc]# ls
7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc-json.log hostconfig.json mounts
checkpoints hostname resolv.conf
config.v2.json hosts resolv.conf.hash
[root@centos8 7854e064cb29902bc1f3fa7e4e70a967e02c9599b4c0a634bf014d9357e60dcc]#
容器“MariaDB_2”的文件config.v2.json中的一些信息:
容器ID : "Config":{"Hostname":"7854e064cb29",
IP地址 : (使用虚拟网卡“staticnet”)
"Networks":{"staticnet":{"IPAMConfig":{"IPv4Address":"172.18.0.3"},"Links":null,"Aliases":["7854e064cb29"],"NetworkID":"2be66df3b3958685e0ec2a62098577ad95590c83d7f3f4c4e967fc9b5626508c","EndpointID":"7c74311cb2b1f1eb11c866591315170545793b09419b9ed1d023d72438b271a9","Gateway":"172.18.0.1","IPAddress":"172.18.0.3","IPPrefixLen":16,
端口映射 :
Ports":{"3306/tcp":[{"HostIp":"0.0.0.0","HostPort":"3307"},{"HostIp":"::","HostPort":"3307"}]},
数据库管理员root@localhost的密码 :
"Env":["MYSQL_ROOT_PASSWORD=P@ssw0rd",
挂载目录的信息 :
"MountPoints":{"/home/database_2":{"Source":"/opt/database_2","Destination":"/home/database_2","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/opt/database_2","Target":"/home/database_2"},
相关文章:
CentOS8安装LAMP+phpMyAdmin
CentOS8_在Docker中限制容器可用的CPU个数和内存量
CentOS8_在Docker中安装Tomcat+MariaDB
CentOS8_在Docker中安装LAMP
CentOS8_在Docker中安装LNMP(使用参数--link)
CentOS8_在Docker中安装LNMP
CentOS8_在Docker中安装Zabbix5.0.12 |