Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1950771
  • 博文数量: 176
  • 博客积分: 1857
  • 博客等级: 上尉
  • 技术积分: 2729
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-14 22:55
个人简介

吾生有涯,而知无涯,适当止学.循序渐进,步步提升 Talk is cheap, show me the code.

文章分类

全部博文(176)

文章存档

2019年(1)

2018年(14)

2017年(20)

2016年(31)

2015年(15)

2014年(5)

2013年(10)

2012年(80)

分类: Mysql/postgreSQL

2012-07-09 09:27:02

replication可以使数据从master服务器复制到一个或者多个slave服务器,replication默认是asynchronous的,
slaves不需要永久的连接去接受来自mater的更新操作。这就意味着updates可以发生在长距离的连接甚至是临时
的或者是间歇性的连接例如拨号服务,根据配置,你可以复制所有的数据库,或者选定的数据库或者复制数据库
里选定的表。
mysql replication的目标是:

Scale-out solutions(可扩展的解决方案):多台slaves可以提高性能,这种环境下,所有的写和更新都可以发
生在master server下,读的功能可以发生在多个salve servers下。
data security(数据安全):因为数据复制到了slave下,然后slave可以停止the replication process,所以它就
可以在不侵染mater数据的情况下进行backup service的操作。
Analytics(善于分析数据):实时的数据发生在master上,然而对于信息的分析师在多台salve上面,所以不会
影响master的任何性能。
Long-distance data distribution(远距离的数据分配):如果一个机构需要你的主数据的copy,你就可以使
用replication给他们创建一个本地的copy而且无需永久的连接在主机上,减少master的负载。

mysql的replication是一个单向的,异步的replication,mysql的cluster是一个同步的replication。但是目前的cluster
的引擎ENGINE=NDBCLUSTER的性能还是不稳定,所以目前流行的还是innodb和MyISAM存储引擎。所以用得最多
应该还是mysql replication(个人的理解,经供参考)
mysql replication里面有两种核心的replication format:Statement Based Replication (SBR),Row Based Replication
(RBR)。也有第三种选择:Mixed Based Replication (MBR)。这个和bin_log的Binary Logging Formats有着异曲同工之妙。

上述为基础理论。开始进行mysql replication的配置

第一步:两台pc:192.168.1.40 (master)
192.168.1.50 (slave)

每台安装相同版本的mysql服务器(我这里把两台linux的iptables都关闭了,这样能让它们互连,虽然关掉防火墙,对信息安全来说是非常不好的,但是这个是一个个人实验,所以仅讲解mysql replication。以简单为主

第二步:Setting the Replication Configuration(Replication的配置)
在master上,配置/etc/my.cnf,添加如下脚本:
  1. [mysqld]
  2. log-bin=mysql-bin
  3. server-id=1
复制代码

在slave上,配置/etc/my.cnf,添加如下脚本:
  1. [mysqld]
  2. server-id=2
复制代码

如上的操作,都要重启一下server。

第三步:Creating a User for Replication(为Replication创建一个用户)
在master上,配置一个新用户,可以让slave服务器能读取master服务器
  1. mysql> CREATE USER 'repl'@'192.168.1.50' IDENTIFIED BY 'repl';
  2. mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.50';
复制代码

第四步:Obtaining the Replication Master Binary Log Coordinates(获得master 二进制日志的坐标)
  1. mysql> FLUSH TABLES WITH READ LOCK;(锁定所有表,不让其他client对此进行写操作)
  2. mysql > SHOW MASTER STATUS;(获得 Master Binary Log Coordinates)
复制代码

第五步:Creating a Data Snapshot Using mysqldump(使用mysqldump获得快照)
这里需要一些讨论:
因为我们要获得基于某一个特定时刻的,数据完整性和一致性的备份集,所以有很多方法获得:

一:通过对数据库停机进行冷备份,然后启动master段的mysql,在client还没有连接进来之前。进行SHOW MASTER STATUS,获得Log Position。
二:如果master是一个需要满足365*24*7服务的数据库,我们数据就必须运行在支持Snapshot
功能的文件系统上面(如ZFS),来获得一个备份集。
具体操作思路:先FLUSH TABLES WITH READ LOCK,然后获得统一的Snapshot,再SHOW MASTER STATUS
获得精确的Log Position
三:就是我们测试实验中要使用的,使用mysqldump客户端进行快照的获得:
  1. mysql> FLUSH TABLES WITH READ LOCK;(锁定所有的表,使得数据一致性和完整性)
  2. shell> mysqldump --master-data -uroot -p zsd >zsd.sql
  3. mysql> UNLOCK TABLES;
复制代码

第六步:Setting Up Replication with New Master and Slaves

Import the dump file:(导入dump文件)
  1. shell> mysql -uroot -p -Dzsd < zsd.sql
复制代码

再进入mysql服务器,执行如下命令:匹配上面import dump file对应的bin_log日志,使得数据一致性。

  1. mysql>CHANGE MASTER TO
  2. MASTER_HOST = '192.168.1.40',(master的ip)
  3. MASTER_PORT = 3306,(master对应的端口)
  4. MSTER_USER = 'repl',(之前赋予可以replication权限的用户)
  5. MASTER_PASSWORD = 'repl',
  6. MASTER_LOG_FILE = 'mysql-bin.000004',(上面dump对应的binlog)
  7. MASTER_LOG_POS = 107;

  8. mysql> START SLAVE;(开启slave)
  9. mysql> show slave status\G;(查看slave状态)
  10. *************************** 1. row ***************************
  11. Slave_IO_State: Waiting for master to send event(等待master传送event,说明正常)
  12. Master_Host: 192.168.1.40
  13. Master_User: repl
  14. Master_Port: 3306
  15. Connect_Retry: 60
  16. Master_Log_File: mysql-bin.000004(与上面的binlog一一对应)
  17. Read_Master_Log_Pos: 107(与上面的binlog的pos一一对应)
  18. Relay_Log_File: localhost-relay-bin.000002
  19. Relay_Log_Pos: 253
  20. Relay_Master_Log_File: mysql-bin.000004
  21. Slave_IO_Running: Yes(这个状态yes,说明正常)
  22. Slave_SQL_Running: Yes(这个状态yes,说明正常)
  23. Replicate_Do_DB:
  24. Replicate_Ignore_DB:
  25. Replicate_Do_Table:
  26. Replicate_Ignore_Table:
  27. Replicate_Wild_Do_Table:
  28. Replicate_Wild_Ignore_Table:
  29. Last_Errno: 0
  30. Last_Error:
  31. Skip_Counter: 0
  32. Exec_Master_Log_Pos: 107
  33. Relay_Log_Space: 413
  34. Until_Condition: None
  35. Until_Log_File:
  36. Until_Log_Pos: 0
  37. Master_SSL_Allowed: No
  38. Master_SSL_CA_File:
  39. Master_SSL_CA_Path:
  40. Master_SSL_Cert:
  41. Master_SSL_Cipher:
  42. Master_SSL_Key:
  43. Seconds_Behind_Master: 0
  44. Master_SSL_Verify_Server_Cert: No
  45. Last_IO_Errno: 0
  46. Last_IO_Error:
  47. Last_SQL_Errno: 0
  48. Last_SQL_Error:
  49. Replicate_Ignore_Server_Ids:
  50. Master_Server_Id: 1
复制代码

测试:我的测试环境中,master服务器里有一个数据库叫zsd,里面对应一张test表,有4条记录,现在我插入一条记录,这时,slave的服务器也会对应的插入一条数据,过程是单向的,异步的。

进入master服务器,进行如下操作:
  1. mysql> use zsd;

  2. mysql> show tables;
  3. +---------------+
  4. | Tables_in_zsd |
  5. +---------------+
  6. | test |
  7. +---------------+
  8. 1 row in set (0.02 sec)
  9. mysql> select * From test;(这里有四条数据)
  10. +------+
  11. | id |
  12. +------+
  13. | 1 |
  14. | 2 |
  15. | 3 |
  16. | 4 |
  17. +------+
  18. 4 rows in set (0.02 sec)
  19. mysql> insert into test values(5);(插入了一条数据)
  20. Query OK, 1 row affected (0.00 sec)
  21. mysql> show master status;(查看master,发现binlog的pos发生了改变)
  22. +------------------+----------+--------------+------------------+
  23. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  24. +------------------+----------+--------------+------------------+
  25. | mysql-bin.000004 | 195 | | |
  26. +------------------+----------+--------------+------------------+
  27. 1 row in set (0.00 sec)
复制代码

在slave服务器,做如下操作
  1. mysql> show slave status\G;(再次查看slave的状态)
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.40
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000004
  9. Read_Master_Log_Pos: 195(这里的binlog与master改变的日志一一对应,说明数据在同步)
  10. Relay_Log_File: localhost-relay-bin.000002
  11. Relay_Log_Pos: 341(这里relay_log_pos也发生了改变)
  12. Relay_Master_Log_File: mysql-bin.000004
  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: 195
  25. Relay_Log_Space: 501
  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. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 1
  43. 1 row in set (0.00 sec)

  44. mysql> use zsd;
  45. Database changed
  46. mysql> select * From test;(查看数据,发现数据多了一条)
  47. +------+
  48. | id |
  49. +------+
  50. | 1 |
  51. | 2 |
  52. | 3 |
  53. | 4 |
  54. | 5 |
  55. +------+
  56. 5 rows in set (0.02 sec):


复制代码

到这里,算是mysql replication安装配置成功。
阅读(3171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~