Docker Mysql 主从配置
环境说明:
采用WSL2 安装Ubuntu,然后在Ubuntu上安装Docker
C:\Users\86189>wsl --list --all 适用于 Linux 的 Windows 子系统分发版: Ubuntu-20.04 (默认) C:\Users\86189>
Ubuntu详细版本
yicheng@LAPTOP-URT4MGDD:~$ uname -a Linux LAPTOP-URT4MGDD 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux yicheng@LAPTOP-URT4MGDD:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.1 LTS Release: 20.04 Codename: focal yicheng@LAPTOP-URT4MGDD:~$
1.查找Docker 资源库
docker search mysql
yicheng@LAPTOP-URT4MGDD:~$ docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10072 [OK] mariadb MariaDB is a community-developed fork of MyS… 3692 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 736 [OK] percona Percona Server is a fork of the MySQL relati… 511 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 83 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 77 centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK] bitnami/mysql Bitnami MySQL Docker Image 45 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] tutum/mysql Base docker image to run a MySQL database se… 35 prom/mysqld-exporter 31 [OK] ...
2.拉取image
yicheng@LAPTOP-URT4MGDD:~$ docker image ls -a REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 9 months ago 13.3kB yicheng@LAPTOP-URT4MGDD:~$ docker pull mysql:8.0.21 8.0.21: Pulling from library/mysql bb79b6b2107f: Pull complete 49e22f6fb9f7: Pull complete 842b1255668c: Pull complete 9f48d1f43000: Pull complete c693f0615bce: Pull complete 8a621b9dbed2: Pull complete 0807d32aef13: Pull complete 9eb4355ba450: Pull complete 6879faad3b6c: Pull complete 164ef92f3887: Pull complete 6e4a6e666228: Pull complete d45dea7731ad: Pull complete Digest: sha256:86b7c83e24c824163927db1016d5ab153a9a04358951be8b236171286e3289a4 Status: Downloaded newer image for mysql:8.0.21 docker.io/library/mysql:8.0.21 yicheng@LAPTOP-URT4MGDD:~$ docker image ls -a REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0.21 8e85dd5c3255 4 days ago 544MB hello-world latest bf756fb1ae65 9 months ago 13.3kB yicheng@LAPTOP-URT4MGDD:~$
3.启动MySQL镜像
master:
docker run -p 3307:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=zen -d mysql:8.0.21
slave:
docker run -p 3308:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=zen -d mysql:8.0.21
yicheng@LAPTOP-URT4MGDD:~$ docker run -p 3307:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=zen -d mysql:8.0.21 a12eb8cd4c19467d6daee6d899b61df3d4c19b0aabde526153de8651676fb505 yicheng@LAPTOP-URT4MGDD:~$ docker run -p 3308:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=zen -d mysql:8.0.21 6d999ccc3c9f360e35131030a849efbde3f2f3b1f535b4ee7231aff017dd77c4 yicheng@LAPTOP-URT4MGDD:~$ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d999ccc3c9f mysql:8.0.21 "docker-entrypoint.s…" 16 seconds ago Up 15 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp mysql-slave a12eb8cd4c19 mysql:8.0.21 "docker-entrypoint.s…" 27 seconds ago Up 27 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql-master d954bf52eb40 hello-world "/hello" 4 weeks ago Exited (0) 4 weeks ago sleepy_chebyshev yicheng@LAPTOP-URT4MGDD:~$
4.用客户端连接MySQL
//宿主机安装mysql 客户端(注意此时的宿主机是Ubuntu不是Windows)
//如果宿主机没有装mysql客户端,先安装一下
sudo apt install mysql-client-core-8.0
连接
mysql -uroot -h127.0.0.1 -P3308 -p
密码:zen 前面指定的
确保两个实例都可以连接上,确保两个实例之间可以连接上(master to slave, slave to master)。
yicheng@LAPTOP-URT4MGDD:~$ mysql -uroot -h127.0.0.1 -P3307 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> exit Bye yicheng@LAPTOP-URT4MGDD:~$ mysql -uroot -h127.0.0.1 -P3308 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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>
5.进入Master容器内部配置my.cnf
docker exec -it mysql-master bash
尝试直接修改 /etc/mysql/my.cnf
yicheng@LAPTOP-URT4MGDD:~$ docker exec -it mysql-master bash root@a12eb8cd4c19:/# cd /etc/mysql root@a12eb8cd4c19:/etc/mysql# ls conf.d my.cnf my.cnf.fallback root@a12eb8cd4c19:/etc/mysql# vi my.cnf bash: vi: command not found root@a12eb8cd4c19:/etc/mysql# apt-get update Err:1 http://deb.debian.org/debian buster InRelease Temporary failure resolving 'deb.debian.org' Err:2 http://security.debian.org/debian-security buster/updates InRelease Temporary failure resolving 'security.debian.org' Err:3 http://repo.mysql.com/apt/debian buster InRelease Temporary failure resolving 'repo.mysql.com' Err:4 http://deb.debian.org/debian buster-updates InRelease Temporary failure resolving 'deb.debian.org' Reading package lists... Done W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease Temporary failure resolving 'deb.debian.org' W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease Temporary failure resolving 'security.debian.org' W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease Temporary failure resolving 'deb.debian.org' W: Failed to fetch http://repo.mysql.com/apt/debian/dists/buster/InRelease Temporary failure resolving 'repo.mysql.com' W: Some index files failed to download. They have been ignored, or old ones used instead. root@a12eb8cd4c19:/etc/mysql# apt-get install vim Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package vim root@a12eb8cd4c19:/etc/mysql#
由于多重桥接网络,导致docker container 内部无法访问外网所以无法更新安装软件。
Docker Container 之间的网络连接相对复杂,后期分单章再表。
转换思路将 my.cnf 文件copy 到 宿主机,修改好后在copy 回来。
yicheng@LAPTOP-URT4MGDD:~$ docker cp mysql-master:/etc/mysql/my.cnf /home/yicheng/ yicheng@LAPTOP-URT4MGDD:~$ ls -al total 44 drwxr-xr-x 3 yicheng docker 4096 Oct 18 14:15 . drwxr-xr-x 3 root root 4096 Sep 13 09:08 .. -rw------- 1 yicheng yicheng 4929 Oct 17 00:24 .bash_history -rw-r--r-- 1 yicheng docker 220 Sep 13 09:08 .bash_logout -rw-r--r-- 1 yicheng docker 3771 Sep 13 09:08 .bashrc drwxr-xr-x 2 yicheng docker 4096 Sep 13 09:10 .landscape -rw-r--r-- 1 yicheng docker 0 Oct 18 09:27 .motd_shown -rw------- 1 yicheng docker 99 Oct 18 10:28 .mysql_history -rw-r--r-- 1 yicheng docker 807 Sep 13 09:08 .profile -rw-r--r-- 1 yicheng docker 0 Sep 13 10:01 .sudo_as_admin_successful -rw------- 1 yicheng docker 683 Oct 18 11:47 .viminfo -rw-rw-r-- 1 yicheng docker 1080 Oct 13 16:01 my.cnf yicheng@LAPTOP-URT4MGDD:~$ vi my.cnf yicheng@LAPTOP-URT4MGDD:~$ cat my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL server-id=1 log-bin=mysql-bin binlog-format=ROW # Custom config should go here !includedir /etc/mysql/conf.d/ yicheng@LAPTOP-URT4MGDD:~$
copy 进去后重启mysql-master 容器,使配置生效
6.创建数据同步账户
docker exec -it mysql-mast bash
mysql -uroot -p
create user 'slave01'@'%' identified by 'slave01';
grant replication slave,replication client on *.* to 'slave01'@'%';
yicheng@LAPTOP-URT4MGDD:~$ docker exec -it mysql-master bash root@a12eb8cd4c19:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> use mysql; Database changed mysql> select host,user from user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | root | | localhost | mysql.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 5 rows in set (0.00 sec) mysql> create user 'slave01'@'%' identified by 'slave01'; Query OK, 0 rows affected (0.01 sec) mysql> grant replication slave,replication client on *.* to 'slave01'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> select host,user from user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | root | | % | slave01 | | localhost | mysql.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 6 rows in set (0.00 sec) mysql>
7.配置slave
同样采用copy 的方式
主要配置以下参数
server-id=201
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=mysql-relay-bin
read_only=1
yicheng@LAPTOP-URT4MGDD:~$ docker cp /home/yicheng/my.cnf mysql-slave:/etc/mysql/ yicheng@LAPTOP-URT4MGDD:~$ docker exec -it mysql-slave bash root@6d999ccc3c9f:/# cd /etc/mysql root@6d999ccc3c9f:/etc/mysql# ls -al total 24 drwxr-xr-x 1 root root 4096 Oct 18 09:32 . drwxr-xr-x 1 root root 4096 Oct 18 02:22 .. drwxrwxr-x 1 root root 4096 Oct 13 08:01 conf.d -rw-rw-r-- 1 1000 mysql 1334 Oct 18 09:31 my.cnf -rw-r--r-- 1 root root 1469 Jun 16 17:16 my.cnf.fallback root@6d999ccc3c9f:/etc/mysql# chown root:root my.cnf root@6d999ccc3c9f:/etc/mysql# ls -al total 24 drwxr-xr-x 1 root root 4096 Oct 18 09:32 . drwxr-xr-x 1 root root 4096 Oct 18 02:22 .. drwxrwxr-x 1 root root 4096 Oct 13 08:01 conf.d -rw-rw-r-- 1 root root 1334 Oct 18 09:31 my.cnf -rw-r--r-- 1 root root 1469 Jun 16 17:16 my.cnf.fallback root@6d999ccc3c9f:/etc/mysql# cat my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL server-id=201 ## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用 log-bin=mysql-slave-bin ## relay_log配置中继日志 relay_log=mysql-relay-bin ## 设置为只读,该项如果不设置,表示slave可读可写 read_only=1 # Custom config should go here !includedir /etc/mysql/conf.d/ root@6d999ccc3c9f:/etc/mysql#
重启mysq-slave 容器。
a.查看容器对应的IP
#只截取了网络部分 yicheng@LAPTOP-URT4MGDD:~$ docker inspect mysql-master "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "1c07ce0ae81eeee8a867ad5f29a318b9a97654153394e95fa7166b38d217d0a5", "EndpointID": "a819e2aa5c78a4323972c07ffbc46607da332d7cd4749454433c0f0c9d8f2d30", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] yicheng@LAPTOP-URT4MGDD:~$
b.查看master 的日志 position
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 724 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.01 sec) mysql>
c.登陆mysql-slave 执行同步参数
change master to
master_host='172.17.0.2' ,
master_user='slave01',
master_password='slave01',
master_port=3306,
master_log_file='mysql-bin.000001',
master_log_pos=724,
master_connect_retry=30;
root@6d999ccc3c9f:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> change master to -> master_host='172.17.0.2', -> master_user='slave01', -> master_password='slave01', -> master_port=3306, -> master_log_file='mysql-bin.000001', -> master_log_pos=724, -> master_connect_retry=30; Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.17.0.2 Master_User: slave01 Master_Port: 3306 Connect_Retry: 30 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 724 Relay_Log_File: 6d999ccc3c9f-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 724 Relay_Log_Space: 156 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) mysql> mysql> start slave; ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository mysql> reset slave; Query OK, 0 rows affected (0.04 sec) mysql> start slave; Query OK, 0 rows affected (0.03 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 172.17.0.2 Master_User: slave01 Master_Port: 3306 Connect_Retry: 30 Master_Log_File: Read_Master_Log_Pos: 4 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 4 Relay_Master_Log_File: Slave_IO_Running: Connecting Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 0 Relay_Log_Space: 156 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 2061 Last_IO_Error: error connecting to master 'slave01@172.17.0.2:3306' - retry-time: 30 retries: 2 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: 201018 10:03:49 Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) mysql>
认证方式有点问题,获取公钥
需要在 slave 上连接 master 获取 public-key
mysql --ssl-mode=DISABLED -h172.17.0.2 -uroot -p --get-server-public-key
连接上后登陆退出,之后查看 slave status 就没有问题了。
root@6d999ccc3c9f:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.17.0.2 Master_User: slave01 Master_Port: 3306 Connect_Retry: 30 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 724 Relay_Log_File: mysql-relay-bin.000003 Relay_Log_Pos: 939 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 724 Relay_Log_Space: 1148 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: bb93eb52-10e8-11eb-b9fd-0242ac110002 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) mysql>
8.验证主从配置
a.在主库上创建数据库,数据表,写入数据
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> create database Tmasterslave character set= 'utf8mb4'; Query OK, 1 row affected (0.02 sec) mysql> create user T800 identified by 'T800'; Query OK, 0 rows affected (0.02 sec) mysql> grant all on Tmasterslave.* to T800; Query OK, 0 rows affected (0.01 sec) mysql> use Tmasterslave; Database changed mysql> create table terminater(id_ int,name_ varchar(128)); Query OK, 0 rows affected (0.08 sec) mysql> insert into terminater(id_,name_) values(1,'T600'); t into terminater(id_,name_) values(2,'T700'); insert into terminater(id_,name_) values(3,'T800'); insert into terminater(id_,name_) values(4,'T1000'); insert into terminater(id_,name_) values(5,'T3000'); insert into terminater(id_,name_) values(6,'T5000'); insert into terminater(id_,name_) values(7,'TX');Query OK, 1 row affected (0.03 sec) mysql> insert into terminater(id_,name_) values(2,'T700'); Query OK, 1 row affected (0.01 sec) mysql> insert into terminater(id_,name_) values(3,'T800'); Query OK, 1 row affected (0.01 sec) mysql> insert into terminater(id_,name_) values(4,'T1000'); Query OK, 1 row affected (0.01 sec) mysql> insert into terminater(id_,name_) values(5,'T3000'); Query OK, 1 row affected (0.01 sec) mysql> insert into terminater(id_,name_) values(6,'T5000'); Query OK, 1 row affected (0.01 sec) mysql> insert into terminater(id_,name_) values(7,'TX'); Query OK, 1 row affected (0.01 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | Tmasterslave | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql>
b.验证从库上的数据
yicheng@LAPTOP-URT4MGDD:~$ docker exec -it mysql-slave bash root@6d999ccc3c9f:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> show databases; +--------------------+ | Database | +--------------------+ | Tmasterslave | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use Tmasterslave; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------------+ | Tables_in_Tmasterslave | +------------------------+ | terminater | +------------------------+ 1 row in set (0.00 sec) mysql> select * from terminater; +------+-------+ | id_ | name_ | +------+-------+ | 1 | T600 | | 2 | T700 | | 3 | T800 | | 4 | T1000 | | 5 | T3000 | | 6 | T5000 | | 7 | TX | +------+-------+ 7 rows in set (0.00 sec) mysql>
至此主从配置成功。下一篇将给大家带来双主配置。
主要参考文档: