ITeye博客

Mysql做主从同步(replication)

162cm · 2008-12-16 19:23 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。