Mysql-主从配置

试验环境:

  主服务器IP:192.168.17.99

  从服务器IP:192.168.17.88

配置:

一、主库

1.1、创建一个复制用户,具有replication slave 权限。

  1. mysql>grant all on *.* to ‘jack@’192.168.17.88′ identified by ‘123; 

1.2、编辑my.cnf文件

  vi /etc/my.cnf

  添加 

  server-id=88   # 标记不同数据库,不能与其他数据库一样

  并开启log-bin二进制日志文件

  log-bin=mysql-bin

  注:需要把默认的server-id=1去掉

  关闭mysql

  [root@cent]  /usr/local/mysql/bin/mysqladmin -uroot -p shutdown  

1.3、启动mysql数据库

  1. /usr/local/mysql/bin/mysqld_safe –user=mysql & 

1.4、得到binlog日志文件名和偏移量

  1. mysql>show master status;  
  2. +——————+———-+————–+——————+  
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  4. +——————+———-+————–+——————+  
  5. | mysql-bin.0000010 | 106| | |  
  6. +——————+———-+————–+——————+ 

二、从库

2.1、编辑my.cnf文件

  vi /etc/my.cnf

  添加

  server-id=88

  log-bin=mysql-bin   #建议开启

  注:需要把默认的server-id=1去掉

  关闭mysql

  /usr/local/mysql/bin/mysqladmin -uroot -p shutdown

2.2、启动从数据库

  1. /usr/local/mysql/bin/mysqld_safe –user=mysql & 

  停止

  mysql>stop slave;//停止从库

2.4、对从数据库进行相应设置

  1. mysql> change master to  
  2. -> master_host=’192.168.17.99′,
  3. -> master_user=’jack’,  
  4. -> master_password=’123’,  
  5. -> master_log_file=’mysql-bin.0000010′,   # 从哪个bin-log开始同步   在主库中show master status
  6. -> master_log_pos=106; 

2.5、启动从服务器slave线程

  1. mysql>start slave; 

2.6、查看slave线程状态

  1. mysql>show slave statusG;  
  2. *************************** 1. row ***************************  
  3. Slave_IO_State: Waiting for master to send event  
  4. Master_Host: 192.168.203.149  
  5. Master_User: repl  
  6. Master_Port: 3306  
  7. Connect_Retry: 60  
  8. Master_Log_File: mysql-bin.0000010  
  9. Read_Master_Log_Pos: 106  
  10. Relay_Log_File: centos-relay-bin.000002  
  11. Relay_Log_Pos: 529  
  12. Relay_Master_Log_File: mysql-bin.0000010  
  13. Slave_IO_Running: Yes  
  14. Slave_SQL_Running: Yes  
  15. Replicate_Do_DB:  
  16. Replicate_Ignore_DB:  
  17. Replicate_Do_Table:  
  18. Replicate_Ignore_Table:  
  19. Replicate_Wild_Do_Table:  
  20. Replicate_Wild_Ignore_Table:  
  21. Last_Errno: 0  
  22. Last_Error:  
  23. Skip_Counter: 0  
  24. Exec_Master_Log_Pos: 106  
  25. Relay_Log_Space: 830  
  26. Until_Condition: None  
  27. Until_Log_File:  
  28. Until_Log_Pos: 0  
  29. Master_SSL_Allowed: No  
  30. Master_SSL_CA_File:  
  31. Master_SSL_CA_Path:  
  32. Master_SSL_Cert:  
  33. Master_SSL_Cipher:  
  34. Master_SSL_Key:  
  35. Seconds_Behind_Master: 0  
  36. Master_SSL_Verify_Server_Cert: No  
  37. Last_IO_Errno: 0  
  38. Last_IO_Error:  
  39. Last_SQL_Errno: 0  
  40. Last_SQL_Error:  
  41. 1 row in set (0.00 sec) 

验证是否配置正确

在从服务器上执行

  1. show slave statusG;  
  2. Waiting for master to send event  
  3. Slave_IO_Running: Yes  
  4. Slave_SQL_Running: Yes 

如以上二行同时为Yes 说明配置成功

测试:

1、在主服务器test数据库中创建user表

  1. mysql>use test;  
  2. mysql>create table user(id int); 

2、在从服务器中查看user表

  1. mysql>use test;  
  2. mysql> show tables like ‘user’;  
  3. +———————-+  
  4. | Tables_in_test(user) |  
  5. +———————-+  
  6. | user |  
  7. +———————-+  
  8. 1 row in set (0.00 sec) 

说明主从数据同步成功。

原文地址:https://www.cnblogs.com/onlyzc/p/8417070.html