主机角色:
主数据库:
[root@A-DB01 ~]# hostname
A-DB01
[root@A-DB01 ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2{print $4}'
172.16.0.14
从数据库:
[root@A-DB02 ~]# hostname
A-DB02
[root@A-DB02 ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2{print $4}'
172.16.0.15
主库操作:
1.主库开binlog功能
[root@A-DB01 ~]# grep "log-bin"/etc/my.cnf
log-bin=mysql-bin
2.确保server-id 不同
[root@A-DB01 ~]# grep server-id /etc/my.cnf
server-id = 1
3. 主库建立同步账号rep
mysql> grant replication slave on *.* torep@"172.16.0.%" identified by 'Lqc123';
Query OK, 0 rows affected (0.00 sec)
查看授权的权限:
mysql> show grants for rep@'172.16.0.%';
mysql> flush privileges;
Query OK,0 rows affected (0.00 sec)
4.锁表,保持数据一致性
flush table with read lock;
锁表之后,重开窗口,操作。
备份
[root@A-DB01 ~]# mysqldump -uroot -p123456 > /root/rep_bak.sql
推到从库。
从库操作:
1.确保所有的server-id不同
[root@A-DB02 ~]# grep server-id /etc/my.cnf
server-id = 2
2.把主库的全备导到从库上
[root@A-DB02 ~]# mysql -uroot -p123456 </root/rep_bak.sql
恢复成功>
3.找位置点,配置master.info
在主库上查看位置点
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 1416 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql-bin.000004 | 1416
在从库上创建
CHANGE MASTER TO
MASTER_HOST='172.16.0.14',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='Lqc123',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=1416;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.0.14
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1416
Relay_Log_File: A-DB02-relay-bin.000002
Relay_Log_Pos: 569
Relay_Master_Log_File: mysql-bin.000004
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: 1416
Relay_Log_Space: 726
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
1 row in set (0.00 sec)
如果change master 里面的东西设置错了,修改master info不生效,请执行
reset slave all;