免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2568 | 回复: 0
打印 上一主题 下一主题

MySQL主从异步复制 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-27 17:25 |只看该作者 |倒序浏览
MySQL主从异步复制









目标:
实现MySQL主从复制,达到实时备份的目的
系统概况:
系统:CentOS;内核版本:2.6.18-194.el5 MySQL: 5.1.54
网络地址:
master:192.168.85.101 slave:192.168.85.102




               一、安装MySQL

               二、配置
               三、验证、测试

一、安装mysql(主从相同)
  1. 01.#useradd mysql -s /sbin/nologin

  2. 02.#tar zxvf mysql-5.1.54.tar.gz

  3. 03.#cd mysql-5.1.54

  4. 04.#./configure --prefix=/usr/local/mysql --localstatedir=/opt/data --with-extra-charsets=utf8,gb2312,gbk --with-pthread --enable-thread-safe-client

  5. 05.#make

  6. 06.#make install

  7. 07.#cp support-files/my-large.cnf /etc/my.cnf

  8. 08.#cd /usr/local/mysql

  9. 09.#chgrp -R mysql .

  10. 10.#/usr/local/mysql/bin/mysql_install_db --user=mysql

  11. 11.# chown -R mysql:mysql /opt/data

  12. 12.#/usr/local/mysql/bin/mysqladmin -u root password sairl

  13. 13.#echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &" >>/etc/rc.local

  14. 14.#echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile

  15. 15.#source /etc/profile
  16. 复制代码
复制代码
二、配置

1)、修改slave服务器的server-id(master服务器不变)
  1. 56  #  required unique id between 1 and 2^32 - 1
  2.   57 # defaults to 1 if  master-host is not set
  3.   58  #  but will not function as a master if omitted
  4.   59  server-id= 10
  5.   60
  6.   61 # Replication Slave  (comment out master section to use this)
复制代码
2)、授权(在master服务器上操作)

  1. 01.mysql> GRANT REPLICATION SLAVE ON *.*

  2. 02.-> TO 'backup'@'192.168.85.102' IDENTIFIED BY 'testpwd';

  3. 03.Query OK, 0 rows affected (0.00 sec)

  4. 04.

  5. 05.mysql> show master status;      //主数据库状态

  6. 06.+------------------+----------+--------------+------------------+

  7. 07.| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

  8. 08.+------------------+----------+--------------+------------------+

  9. 09.| mysql-bin.000003 |      409 |              |                  |

  10. 10.+------------------+----------+--------------+------------------+
  11. 复制代码
复制代码
3)、配置slave服务器
  1. 01.mysql> change master to

  2. 02.-> master_host='192.168.85.101',

  3. 03.-> master_user='backup',

  4. 04.-> master_password='testpwd',

  5. 05.-> master_log_file='mysql-bin.000003',

  6. 06.-> master_log_pos=409;

  7. 07.Query OK, 0 rows affected (0.00 sec)

  8. 08.mysql> start slave;

  9. 09.Query OK, 0 rows affected (0.00 sec)
复制代码
复制代码

查看一下同步状态
  1. 01.mysql> show slave status\G

  2. 02.*************************** 1. row ***************************

  3. 03.               Slave_IO_State: Waiting for master to send event

  4. 04.                  Master_Host: 192.168.85.101

  5. 05.                  Master_User: replication

  6. 06.                  Master_Port: 3306

  7. 07.                Connect_Retry: 60

  8. 08.              Master_Log_File: mysql-bin.000003

  9. 09.          Read_Master_Log_Pos: 720

  10. 10.               Relay_Log_File: sairl-DB-2-relay-bin.000008

  11. 11.                Relay_Log_Pos: 251

  12. 12.        Relay_Master_Log_File: mysql-bin.000003

  13. 13.             Slave_IO_Running: Yes

  14. 14.            Slave_SQL_Running: Yes

  15. 15.              Replicate_Do_DB:

  16. 16.          Replicate_Ignore_DB:

  17. 17.           Replicate_Do_Table:

  18. 18.       Replicate_Ignore_Table:

  19. 19.      Replicate_Wild_Do_Table:

  20. 20.  Replicate_Wild_Ignore_Table:

  21. 21.                   Last_Errno: 0

  22. 22.                   Last_Error:

  23. 23.                 Skip_Counter: 0

  24. 24.          Exec_Master_Log_Pos: 720

  25. 25.              Relay_Log_Space: 556

  26. 26.              Until_Condition: None

  27. 27.               Until_Log_File:

  28. 28.                Until_Log_Pos: 0

  29. 29.           Master_SSL_Allowed: No

  30. 30.           Master_SSL_CA_File:

  31. 31.           Master_SSL_CA_Path:

  32. 32.              Master_SSL_Cert:

  33. 33.            Master_SSL_Cipher:

  34. 34.               Master_SSL_Key:

  35. 35.        Seconds_Behind_Master: 0

  36. 36.Master_SSL_Verify_Server_Cert: No

  37. 37.                Last_IO_Errno: 0

  38. 38.                Last_IO_Error:

  39. 39.               Last_SQL_Errno: 0

  40. 40.               Last_SQL_Error:

  41. 41.1 row in set (0.00 sec)
复制代码
复制代码

PS:slave_IO进程及slave_SQL进程都必须正常运行 

 


三、验证、测试
主从复制测试:在master数据库服务器上创建库和表,然后再插入记录,再登陆到slave服务器,看是否也建立相一致的库和表以及记录。
  1. 01.mysql> create database test_db;

  2. 02.Query OK, 1 row affected (0.00 sec)

  3. 03.

  4. 04.mysql> use test_db;

  5. 05.Database changed

  6. 06.mysql> create table test_table(id int(5),name char(10));

  7. 07.Query OK, 0 rows affected (0.00 sec)

  8. 08.

  9. 09.mysql> insert into test_table

  10. 10.    -> values(01,'xiujie');

  11. 11.Query OK, 1 row affected (0.00 sec)

  12. 12.

  13. 13.现在登录slave数据库服务器,看是否成功同步。

  14. 14.mysql> show databases;

  15. 15.+--------------------+

  16. 16.| Database           |

  17. 17.+--------------------+

  18. 18.| information_schema |

  19. 19.| mysql              |

  20. 20.| test               |

  21. 21.| test_db            |

  22. 22.+--------------------+

  23. 23.4 rows in set (0.02 sec)

  24. 24.

  25. 25.mysql> use test_db;

  26. 26.Database changed

  27. 27.mysql> show tables;

  28. 28.+-------------------+

  29. 29.| Tables_in_test_db |

  30. 30.+-------------------+

  31. 31.| test_table        |

  32. 32.+-------------------+

  33. 33.1 row in set (0.00 sec)

  34. 34.

  35. 35.mysql> select * from test_table;

  36. 36.+------+--------+

  37. 37.| id   | name   |

  38. 38.+------+--------+

  39. 39.|    1 | xiujie |

  40. 40.+------+--------+

  41. 41.1 row in set (0.00 sec)
复制代码
复制代码
OK,配置到此结束

201106~1.JPG (31.58 KB, 下载次数: 4)

201106~1.JPG
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP