版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、手把手教你实现MySQL双机数据同步假设目前有两台 MySQL 数据库服务器,如何实现这两台机器的数据同步问题?很多朋友一开始接触MySQL双机同步需求的时候可能会感到不知道从哪里入手,事实上这是MySQL本身就支持的功能之一。本文提供有关MySQL主从同步的初步思路,供大家参考。AD: 编者按:很多朋友一开始接触MySQL双机同步需求的时候可能会感到不知道从哪里入手,事实上这是MySQL本身就支持的功能之一。本文提供有关MySQL主从同步的初步思路,供大家参考。一.需求问题假设目前有两台 MySQL 数据库服务器,如何实现这两台机器的数据同步问题?即在一台机器上修改数据库后,另一台机器会同步
2、更新所修改的信息。二.解决方案查资料发现 MySQL 支持单向,异步复制,复制过程中一个服务器充当主服务器,而另一个或多个其他服务器充当从服务器。原理是这样的:主服务器将更新写入二进制日志文件,并维护文件的一个索引来跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时, 它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接受从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。2.1 测试环境1. Master : 7 (CentOS 5.5 x86_64
3、160;) MySQL Version : 5.0.77 2. Slave: 03 (CentOS 5.3 i386) MySQL Version : 5.0.45 备注:Master 和 slave 端的 MySQL 版本最好要一样的,或者 Master 端的版本高于 Slave 端2.2 配置过程2.2.1 Master 端设置开启 MySQL 服务并新建
4、一个测试数据库 abc:1. rootcamlit : /etc/init.d/mysqld start 2. jian.macamlit : mysql -u root -p 3. Enter password: xxxx 4. Welcome to the MySQL monitor. Commands end with
5、0;or g. 5. Your MySQL connection id is 3 6. Server version: 5.0.77 Source distribution 7. 8. Type 'help;' or 'h' for help. Type 'c' to clea
6、r the buffer. 9. 10. mysql> create database abc; 11. Query OK, 1 row affected (0.31 sec) 12. 13. #创建一个用来同步的用户,指定只能在 03 登录 14. #REPLICATION SLAVE: Enable repl
7、ication slaves to read binary log events from the master 15. 16. mysql> grant replication slave on *.* to 'test1''03' identified by 'test1' 17
8、. Query OK, 0 rows affected (0.16 sec) 修改配置文件:1. rootcamlit : vi /etc/f 备注:在修改配置文件之前做好该文件的备份工作。1. mysqld 2. datadir=/var/lib/mysql 3. socket=/var/lib/mysql/mysql.sock 4. user=mysql 5. old_pas
9、swords=1 6. 7. #增加下面内容 8. server_id=1#1 表示 master, 2 表示 slave binlog-do-db=abc #需要同步的数据库,如果有多个数据库,每个数据库一行 binlog-ignore-db=mysql#不需要同步的数据库 log-bin=mysql-bin 9. 10. mysqld_safe
10、11. log-error=/var/log/mysqld.log 12. pid-file=/var/run/mysqld/mysqld.pid 重启服务:1. rootcamlit : /etc/init.d/mysqld restart 2.2.2 Slave 端设置和 master 端一样创建一个相同的数据库: abc1. Enter password: 2. Welcome to the MySQL m
11、onitor. Commands end with or g. 3. Your MySQL connection id is 5 4. Server version: 5.0.45-log Source distribution 5. 6. Type 'help;' or 'h
12、9; for help. Type 'c' to clear the buffer. 7. 8. mysql> create database abc; 9. Query OK, 1 row affected (0.31 sec) 修改配置文件:1. roottest2 : vi /etc/f
13、 1. mysqld 2. datadir=/var/lib/mysql 3. socket=/var/lib/mysql/mysql.sock 4. user=mysql 5. old_passwords=1 6. 7. #增加下面内容 8. server_id=2 log-bin=mysql-bin master-host=7
14、; master-user=test1 master-password=test1 master-port=3306 master-connect-retry=10 #连接次数 replicate-do-db=abc #接受的数据库名 replicate-ignore-db=mysql #不要接受的数据库 9. 1
15、0. mysqld_safe 11. log-error=/var/log/mysqld.log 12. pid-file=/var/run/mysqld/mysqld.pid 重启服务:1. roottest2: /etc/init.d/mysqld restart 备注:配置成功 后会在 mysql 目录(/var/lib/mysql/)下生成 文件,如果要更改 slave 设置,要先将该文件删除才会起作用。进入 mysql,输入下面命令:1. roott
16、est2: mysql -u root -p 2. Enter password: 3. Welcome to the MySQL monitor. Commands end with or g. 4. Your MySQL connection id is 4 5. Server vers
17、ion: 5.0.45-log Source distribution 6. 7. Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 8. 9. mysql> slave start; 10.
18、 Query OK, 0 rows affected, 1 warning (0.00 sec) 11. #查看同步情况 12. mysql > show slave status; 或 show master status; 2.3 结果测试在 Master 端进行数据库 abc 的一些操作,如下所示:1. jian.macamlit :
19、mysql -u root -p 2. Enter password: 3. Welcome to the MySQL monitor. Commands end with or g. 4. Your MySQL connection id is 3 5. Server ver
20、sion: 5.0.77-log Source distribution 6. 7. Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 8. 9. mysql> use abc; 10. Da
21、tabase changed 11. mysql> create table test1 (IP VARCHAR(20),USER VARCHAR(100), MAIL 12. VARCHAR(100); 13. Query OK, 0 rows affected (1.20 sec) 14. mysql> insert into
22、 test1(IP,USER,MAIL) values('6', 'test', 'test'); 15. Query OK, 1 row affected (0.06 sec) 在 Slave 端查看是否能够更新:1. roottest2 : mysql -u root -p 2. Enter p
23、assword: 3. Welcome to the MySQL monitor. Commands end with or g. 4. Your MySQL connection id is 6 5. Server version: 5.0.45-log Source distribution 6
24、. 7. Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 8. 9. mysql> show databases; 10. +-+ 11. | Database |
25、 12. +-+ 13. | information_schema | 14. | foo| 15. | mysql | 16. | test | 17. |abc | 18. +-+ 19. 5rows in set (0.00 sec)
26、 20. mysql> use abc; 21. Reading table information for completion of table and column names 22. You can turn off this feature to get a quicker startup with
27、160;-A 23. Database changed 24. mysql> show tables; 25. +-+ 26. | Tables_in_abc | 27. +-+ 28. | test1 | 29. +-+ 30. 1 row in set (0.03 sec)
28、160; 31. mysql> select * from test1; 32. +-+-+-+ 33. | IP | USER | MAIL | 34. +-+-+-+ 35. | 6 | test | test | 36. +-+-+-+&
29、#160; 37. 1 row in set (0.00 sec) 从上面的结果可以看到 Master 端的数据可以同步到 Slave 端里面。说明此时主从数据库的同步问题已经成功解决。三.补充资料关于如何连接到远程 MySQL 问题,可以采取下面的步骤:首先先登录到远程机器:1. jian.macamlit : ssh root03 2. password: xxx 3. roottest2 :
30、60; 编辑配置文件:1. roottest2 : vi /etc/f 增加下面一行内容:1. mysqld 2. datadir=/var/lib/mysql 3. socket=/var/lib/mysql/mysql.sock 4. user=mysql 5. old_passwords=1 6. bind-address=03#此 IP 地址为 MySQ
31、L 本机的 IP 地址 7. mysqld_safe 8. log-error=/var/log/mysqld.log 9. pid-file=/var/run/mysqld/mysqld.pi 重启服务:1. roottest2 : /etc/init.d/mysqld restart 创建测试数据库:1. roottest2 : mysql -u root -p 2. Enter
32、 password: 3. Welcome to the MySQL monitor. Commands end with or g. 4. Your MySQL connection id is 2 5. Server version: 5.0.45 Source distribution
33、0;6. 7. Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 8. 9. mysql> create database foo 10. Query OK, 1 row affected
34、0;(0.00 sec) 11. #增加用户 test123 从任何主机登录到 MySQL 12. mysql> grant all privileges on *.* to 'test123''%' identified by 'test123' with grant option; 13. Quer
35、y OK, 0 rows affected (0.00 sec) 14. #增加用户 test1 从 7 主机登录到 MySQL 15. mysql> grant all privileges on foo.* to 'test1''7' identified by
36、39;test1' with grant option; 16. Query OK, 0 rows affected (0.00 sec) 如果有防火墙的设置的话,可以如下设置:1. roottest2 : iptables -A INPUT -i eth0 -s 7 -p tcp -dport
37、160;3306 -j ACCEPT 2. roottest2: /etc/init.d/iptales save 最后在客户端就可以输入下面命令来远程进入 MySQL 数据库:1. jian.macamlit : mysql -u test1 -h 03 -p 2. Enter password: 3. Welcome to the
38、60;MySQL monitor. Commands end with or g. 4. Your MySQL connection id is 13 5. Server version: 5.0.45 Source distribution 6. Type 'help;' or 'h'
39、160;for help. Type 'c' to clear the buffer. mysqld.pl内容如下:1. #!/usr/bin/perl 2. 3. #This script is used to check if the mysql replication is ok 4. 5. use strict; 6.
40、use DBI; 7. use POSIX "strftime" 8. 9. my $host = "03" 10. my $user = "test1&
41、quot; 11. my $passwd = "test1" 12. my $port = "3306" 13. my $max_behind = "120"
42、; 14. my $check_log = "./mysql_check.log" 15. 16. 17. #Open the log file 18. open (FH, ">> $check_log") or die $!; 19. 20. #Connect the
43、 mysql server 21. my $dbh = &MysqlConnect ($host, $port, $user, $passwd); 22. 23. 24. #Get slave sql status 25. my $slave_status = &MysqlQuery( $dbh, 'show slave
44、0;status'); 26. print FH "Error: SQL Query Error:" . $dbh->errstr; 27. 28. 29. my $slave_IO = $slave_status->Slave_IO_Running;
45、0;30. my $slave_SQL = $slave_status->Slave_SQL_Running; 31. my $seconds_behind_master = $slave_status->Seconds_Behind_Master; 32. my $now_time
46、; = POSIX:strftime ("%Y-%m-%d %H:%M:%S", localtime); 33. 34. 35. print "Check the Slave MySQL stauts.n" 36. print "_" x 50, "n" 37.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年怀化市鹤中一体化发展事务中心公开选调工作人员备考题库及1套参考答案详解
- 校园欺凌事件的学校管理与学生心理干预研究教学研究课题报告
- 2026年【山西】山西德瑞技工学校教师招聘备考题库及参考答案详解一套
- 中国电子科技集团公司第四十、四十一研究所2026届校园招聘备考题库及参考答案详解1套
- 贵州警察学院《计算机基础》2023-2024学年第一学期期末试卷
- 贵阳人文科技学院《中国近代史纲要》2023-2024学年第一学期期末试卷
- 辽宁中医药大学杏林学院《形势与政策》2023-2024学年第一学期期末试卷
- 2025年停车场节能减排技术行业报告
- 2026年浙江高信技术股份有限公司招聘备考题库(含答案详解)
- 自然资源部所属单位2026年度公开招聘工作人员备考题库(第一批)及参考答案详解1套
- 洪恩识字1-1300字文档
- 社区楼道长管理制度
- 公考培训机构班级管理制度
- 辽宁历届高考状元一览表
- 环泊酚的临床应用及注意事项
- 浅谈施工组织设计的优化
- 工程伦理与管理知到智慧树章节测试课后答案2024年秋山东大学
- 2024年互联网+医疗健康产业合作框架协议
- 老房子维修安全协议书(2篇)
- 教育信息化促进乡村教育振兴
- DB45-T 2854-2024 侗族服饰标准规范
评论
0/150
提交评论