MySQL的主从关系拓扑图:
1.修改主机名称,映射主机名
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# hostnamectl set-hostname slave
[root@master ~]# echo '172.25.0.13 slave' >> /etc/hosts #映射slave主机名
[root@slave ~]# echo '172.25.0.11 master' >> /etc/hosts #映射master主机名
[root@master ~]# ping slave -c 4
PING slave (172.25.0.13) 56(84) bytes of data.
64 bytes from node3 (172.25.0.13): icmp_seq=1 ttl=64 time=2.91 ms
64 bytes from node3 (172.25.0.13): icmp_seq=2 ttl=64 time=0.807 ms
64 bytes from node3 (172.25.0.13): icmp_seq=3 ttl=64 time=0.955 ms
64 bytes from node3 (172.25.0.13): icmp_seq=4 ttl=64 time=0.813 ms
--- slave ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3009ms
rtt min/avg/max/mdev = 0.807/1.373/2.917/0.893 ms
[root@slave ~]# ping master -c 4
PING master (172.25.0.11) 56(84) bytes of data.
64 bytes from node1 (172.25.0.11): icmp_seq=1 ttl=64 time=0.695 ms
64 bytes from node1 (172.25.0.11): icmp_seq=2 ttl=64 time=1.04 ms
64 bytes from node1 (172.25.0.11): icmp_seq=3 ttl=64 time=0.978 ms
64 bytes from node1 (172.25.0.11): icmp_seq=4 ttl=64 time=1.23 ms
--- master ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.695/0.987/1.230/0.196 ms
master的ip: 172.25.0.11 主节点
slave的ip: 172.25.0.13 从节点
2.在Master(主节点)创建一个test1数据库和数据表进行实验
#[root@master ~]# mysql -u root -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database test1;
Query OK, 1 row affected (0.02 sec)
mysql> use test1;
Database changed
mysql> create table student (id int(11) not null)charset=utf8;
Query OK, 0 rows affected, 1 warning (0.09 sec)
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql>
2-1.还没有安装MySQL数据库的请看上一篇:https://blog.csdn.net/qq_41709494/article/details/94042008
3.配置master的同步数据名称和二进制日志
[root@master ~]# systemctl stop mysqld #配置前先停掉服务
[root@master ~]# vi /etc/my.cnf
[mysqld]
...
log-bin=mysqllog #启用二进制日志,默认存在/var/lib/mysql
server-id=1 #本机数据库id唯一标识
binlog-do-db=test1 #可以被从服务器复制的库
[root@master ~]# systemctl restart mysqld
4.Mater(主)数据库 授权(Slave)从数据库
#[root@master ~]# mysql -u root -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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> create user 'slave'@'slave' identified by '123456'; #创建slave用户数据库,设置密码为:123456
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'slave'@'slave' ; #授权slave用户数据库的异步复制权限
Query OK, 0 rows affected (0.01 sec)
5.在slave(从节点)上测实登录
[root@slave ~]# mysql -h master -u slave -p123456
mysql: [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 9
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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>
6.在master (主节点)导出所有数据库,并远程复制到slave(从节点)上
[root@master ~]# mysqldump -u root -p -A > test1.sql #导出master(主节点)的所有数据库
Enter password:
[root@master ~]# ls test1.sql #查看数据库是否存在
test1.sql
[root@master ~]# scp -r test1.sql root@slave:/root #远程复制到slave(从节点)上
The authenticity of host 'slave (172.25.0.13)' can't be established.
ECDSA key fingerprint is SHA256:D7R6A1AAbPiyPsifOQVfgzdx9xI7LdoCKw0aZ1SxRUo.
ECDSA key fingerprint is MD5:2d:0b:01:47:6e:01:f3:7e:06:14:7a:b4:dd:69:d6:71.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave' (ECDSA) to the list of known hosts.
root@slave's password:
test1.sql 100% 900KB 34.4MB/s 00:00
7.在slave(从节点)导入master(主节点)的所有数据库
[root@slave ~]# mysql -u root -p < test1.sql #导入master(主节点)数据库
Enter password:
[root@slave ~]#
[root@slave ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.01 sec)
mysql> use test1;
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_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql>
7.配置Slave(从节点)
[root@slave ~]# vi /etc/my.cnf
[mysqld]
...
server-id=2 #服务的标识符
replicate-do-db=test1 #指定Master(主节点)同步数据库名称
log-bin=mysqllog #指定Master(主节点)的日志名称
[root@slave ~]# systemctl restart mysqld #配置完后要重启服务
8.在Master(主节点)查看数据库状态
[root@master ~]# mysql -u root -p123456
mysql: [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 8
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysqllog.000003 | 155 | test1 | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
9.在Slave(从节点)上的数据库配置
[root@slave ~]# mysql -u root -p123456
mysql: [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 10
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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
Empty set (0.00 sec)
mysql> change master to
-> master_host='master', #要对应Master的主机名(ip)
-> master_user='slave', #要对应Master的数据库的(slave)用户
-> master_password='123456', #要对应Master的数据库的(slave)用户的密码
-> master_log_file='mysqllog.000003', #要对应Master(主节点)的日志文件
-> master_log_pos=155; #要对应Master(主节点)的日志文件的位置的号码
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave; #启动slave(从节点)数据库的同步数据
Query OK, 0 rows affected (0.01 sec)
10.查看Slave(从节点)上的状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqllog.000003
Read_Master_Log_Pos: 435
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 601
Relay_Master_Log_File: mysqllog.000003
Slave_IO_Running: Yes #IO_Running为yes才可以与Mater(主节点)的io流通信
Slave_SQL_Running: Yes #SQL_Running为从节点(slave)的运行状态
Replicate_Do_DB: test1
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: 435
Relay_Log_Space: 809
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: a1d24e8a-9ca0-11e9-aa59-000c296fe7b6
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)
11. 在Master(主节点)测实,插入一条数据是否在Slave(从节点)上可以实现同步数据
[root@master ~]# mysql -u root -p123456
mysql: [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 43
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test1;
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_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql> insert into test1.student values(10);
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+
| id |
+----+
| 10 |
+----+
1 row in set (0.00 sec)
12.在Slave(从节点)查看数据库是否实现同步数据
[root@slave ~]# mysql -u root -p123456
mysql: [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 26
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test1;
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_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.01 sec)
mysql> select * from student;
+----+
| id |
+----+
| 10 |
+----+
1 row in set (0.00 sec)