master | 192.168.1.153 |
slave | 192.168.1.154 |
os | rhel 7.2 |
1、修改 master
# vi /etc/my.cnf
[mysqld] log-bin=mysql-bin server-id=153 |
# systemctl restart mysqld # 重启
2、修改 slave
# vi /etc/my.cnf
[mysqld] log-bin=mysql-bin server-id=154 |
# systemctl restart mysqld # 重启
3、master 建立账户并授权slave
mysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'192.168.1.%' identified by 'Ma991218##'; # 新建 mysync 用户
mysql> show master status; # 查看 master 状态,出来的结果在 slave 有用
+------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 449 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) |
4、配置 slave
mysql> change master to master_host='192.168.1.153',master_user='mysync',master_password='Ma991218##',master_log_file='mysql-bin.000001',master_log_pos=449;
mysql> start slave; # 开启从服务器复制功能
mysql> show slave status\G # 查看 slave 状态,要看到两个 YES
*************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.1.153 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 449 Relay_Log_File: mysql-02-relay-bin.000001 Relay_Log_Pos: 4 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: 449 Relay_Log_Space: 154 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: 1130 Last_IO_Error: error connecting to master 'mysync@192.168.1.153:3306' - retry-time: 60 retries: 1 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: /var/lib/mysql/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: 171010 08:17:56 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.00 sec) |
5、测试
# master 操作,新建库、表
mysql>create database ceshi
mysql> use ceshi
mysql> create table student(id int(10) primary key auto_increment,name varchar(30),agetinyint(2));
mysql> insert into student (id,name,age) value(321281,"mzh",18);
# slave 操作,查看有无同步
mysql> show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | ceshi | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) |
mysql> use ceshi
mysql> show tables;
+-----------------+ | Tables_in_ceshi | +-----------------+ | cs | | student | +-----------------+ 2 rows in set (0.00 sec) |
mysql> select * from student; # 已同步
+--------+------+------+ | id | name | age | +--------+------+------+ | 321281 | mzh | 18 | +--------+------+------+ 1 row in set (0.01 sec) |