mysql 搭建主从复制
在MySQL安装成功基础上
环境:
主 master : 192.168.1.200 centos 7.6 mysql 5.7.17 从 node1 : 192.168.1.201 centos 7.6 mysql 5.7.17
主 master配置:my.cnf
[root@master mysql]# pwd /usr/local/mysql [root@master mysql]# ls bin docs man mysql.sock.lock support-files COPYING include my.cnf README data lib mysql.sock share [root@master mysql]# vim my.cnf ... [mysqld] server-id=1 #数据库唯一ID log-bin = mysql-bin #开启二进制日志功能
....
重启MySQL
[root@master mysql]# systemctl restart mysql
创建同步用户
[root@master mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17 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> CREATE USER 'node1'@'192.168.1.201' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'node1'@'192.168.1.201'; Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看File列和Position列的值
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
从 mnode1配置:my.cnf
[root@node1 mysql]# vim my.cnf
...
[mysqld] server-id=2 #设置从服务器slave的id log-bin=mysql-bin #如果此slave是其他slave的master,则此项也必须开启 relay_log=mysql-relay-bin #配置二进制relay日志命名格式 log_slave_updates=1 #表示slave将复制事件写进自己的二进制日志 read_only=1
...
[root@node1 mysql]# mysql -uroot -p
Enter password:
mysql> change master to master_host='192.168.1.200',
-> master_user='node1',
-> master_password='123456',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=154;
mysql> show slave status\G #查看slave状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.200
Master_User: node1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
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: 154
Relay_Log_Space: 740
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: 9af01d49-6ed1-11ec-831b-000c296fc15e
Master_Info_File: /usr/local/mysql/data/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:
1 row in set (0.01 sec)
重启
[root@node1 mysql]# systemctl restart mysql
[root@node1 ~]# mysql -uroot -p
Enter password: mysql> start slave;
mysql> show slave status\G #查看slave状态
验证:在主 master 创建测试数据
[root@master mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.17-log 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> create database test; Query OK, 1 row affected (0.05 sec) mysql> use test; Database changed mysql> create table user(id int(5),name char(10)); Query OK, 0 rows affected (0.18 sec) mysql> insert into user values (2020001,'shanghai'); Query OK, 1 row affected (0.04 sec) mysql> insert into user values (2020002,'yunnan'); Query OK, 1 row affected (0.05 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql |
| performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec) mysql> select * from test.user; +---------+----------+ | id | name | +---------+----------+ | 2020001 | shanghai | | 2020002 | yunnan | +---------+----------+ 2 rows in set (0.00 sec)
验证:从 node1 获取测试数据
[root@node1 mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.7.17-log 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysqltest | | sys | | test | +--------------------+ 5 rows in set (0.01 sec) mysql> select * from test.user; +---------+----------+ | id | name | +---------+----------+ | 2020001 | shanghai | | 2020002 | yunnan | +---------+----------+ 2 rows in set (0.01 sec)
至此,主从复制!