`
162cm
  • 浏览: 51890 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Mysql做主从同步(replication)

阅读更多

原文地址:
http://www.162cm.com/archives/740.html

哈,原来手里有一份教程, 每次做同步都照做,一直很顺利。这次很久没做同步了,竟然不会了。折腾了好久,特地做个笔记。

   1. Mysql 同步笔记
   2. 1.a为主,b为从.
   3. a的ip:10.62.240.128
   4. b的ip:10.62.240.126
   5. 
   6. a,b 两台机器均装好Mysql,同时确保配置文件中
   7. character_set_server                = utf8
   8. collation_server                    = utf8_unicode_ci
   9. 两台机器的设置相同。
  10. 
  11. 2.在主(a)上配置:
  12. [/etc/my.cnf]中mysqld域新增:
  13. log-bin
  14. server-id       = 1
  15. sql-bin-update-same
  16. binlog-do-db    = database_name
  17. 3.在从机器中配置:
  18. master-host=10.62.240.128          #a的ip
  19. master-user=replication
  20. master-password=replication
  21. master-port=3306
  22. server-id=2
  23. master-connect-retry=60
  24. replicate-do-db=database_name
  25. log-slave-updates        
  26. 4.授权:在a上面进入mysql:
  27. GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.62.240.126' IDENTIFIED BY 'replication';
  28. flush privileges;
  29. 5.锁定a:
  30. mysql> use database_name;
  31. mysql>FLUSH TABLES WITH READ LOCK; #锁定要同步的test表,然后导出数据结构
  32. mysql>show master status;#记录这里的结果。记录如下:
  33. mysqld-bin.000003 |  1399444 | reping       |
  34. mysqldump database_name > dump.sql
  35. mysql> UNLOCK TABLES;                         #已做好同步数据库结构导出后,解锁这个表
  36. 
  37. 6.在b(从)中载入sql:
  38. mysql -uroot < dump.sql
  39. 7:在b中对replication位置进行设定:
  40. mysql>change master to master_host='10.62.240.128', master_port=3306, master_user='replication',master_password='replication',master_log_file='mysqld-bin.000003', MASTER_LOG_POS=1399444;
  41. 
  42. mysql>start slave;#启动复制服务
  43. 8:检查主和从之间的同步是否已经跟上:
  44. 在a中运行mysql>show master status;
  45. mysql> show master status;
  46. +-------------------+----------+--------------+------------------+
  47. | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  48. +-------------------+----------+--------------+------------------+
  49. | mysqld-bin.000003 |  5614301 | reping       |                
  50. 再在从中运行msyql> show  slave status;
  51. show slave status\G
  52. *************************** 1. row ***************************
  53.              Slave_IO_State: Waiting for master to send event
  54.                 Master_Host: 10.62.240.128
  55.                 Master_User: replication
  56.                 Master_Port: 3306
  57.               Connect_Retry: 60
  58.             Master_Log_File: mysqld-bin.000003
  59.         Read_Master_Log_Pos: 5614301
  60.              Relay_Log_File: mysqld-relay-bin.000001
  61.               Relay_Log_Pos: 4214905
  62.       Relay_Master_Log_File: mysqld-bin.000003
  63.            Slave_IO_Running: Yes
  64.           Slave_SQL_Running: Yes
  65.             Replicate_Do_DB:
  66.         Replicate_Ignore_DB:
  67.          Replicate_Do_Table:
  68.      Replicate_Ignore_Table:
  69.     Replicate_Wild_Do_Table:
  70. Replicate_Wild_Ignore_Table:
  71.                  Last_Errno: 0
  72.                  Last_Error:
  73.                Skip_Counter: 0
  74.         Exec_Master_Log_Pos: 5614301
  75.             Relay_Log_Space: 4214905
  76.             Until_Condition: None
  77.              Until_Log_File:
  78.               Until_Log_Pos: 0
  79.          Master_SSL_Allowed: No
  80.          Master_SSL_CA_File:
  81.          Master_SSL_CA_Path:
  82.             Master_SSL_Cert:
  83.           Master_SSL_Cipher:
  84.              Master_SSL_Key:
  85.       Seconds_Behind_Master: 0
  86. 1 row in set (0.01 sec)
  87. 
  88. 
  89. 注意read_master_log_pos也是5614301,说明复制成功。
  90. 以后复制出问题时,也是用这种方式查看和修改。那种查入一条记录然后看从库中是不是也执行了的办法,不可取。

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics