免费注册 查看新帖 |

Chinaunix

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

MySQL主从异步复制【转】 [复制链接]

论坛徽章:
1
子鼠
日期:2014-05-04 13:59:31
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-06 00:15 |只看该作者 |倒序浏览
修杰_JIANG
                                                                                                www.geeux.com



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


                       
                        一、安装MySQL
                        二、配置
                        三、验证、测试





一、安装mysql(主从相同)
  1. #useradd mysql -s /sbin/nologin
  2. #tar zxvf mysql-5.1.54.tar.gz
  3. #cd mysql-5.1.54
  4. #./configure --prefix=/usr/local/mysql --localstatedir=/opt/data --with-extra-charsets=utf8,gb2312,gbk --with-pthread --enable-thread-safe-client
  5. #make
  6. #make install
  7. #cp support-files/my-large.cnf /etc/my.cnf
  8. #cd /usr/local/mysql
  9. # chgrp -R mysql .
  10. #/usr/local/mysql/bin/mysql_install_db --user=mysql
  11. # chown -R mysql:mysql /opt/data
  12. # /usr/local/mysql/bin/mysqld_safe --user=mysql &
  13. #/usr/local/mysql/bin/mysqladmin -u root password sairl
  14. #echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &" >>/etc/rc.local
  15. #echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
  16. #source /etc/profile
复制代码
二、配置

1)、修改slav服务器的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. mysql> GRANT REPLICATION SLAVE ON *.*
  2. -> TO 'backup'@'192.168.85.102' IDENTIFIED BY 'testpwd';
  3. Query OK, 0 rows affected (0.00 sec)

  4. mysql> show master status;      //主数据库状态
  5. +------------------+----------+--------------+------------------+
  6. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  7. +------------------+----------+--------------+------------------+
  8. | mysql-bin.000003 |      409 |              |                  |
  9. +------------------+----------+--------------+------------------+
复制代码
3)、配置slave服务器
  1. mysql> change master to
  2. -> master_host='192.168.85.101',
  3. -> master_user='backup',
  4. -> master_password='testpwd',
  5. -> master_log_file='mysql-bin.000003',
  6. -> master_log_pos=409;
  7. Query OK, 0 rows affected (0.00 sec)
  8. mysql> start slave;
  9. Query OK, 0 rows affected (0.00 sec)
复制代码
查看一下同步状态
  1. mysql> show slave status\G
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.85.101
  5.                   Master_User: replication
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: mysql-bin.000003
  9.           Read_Master_Log_Pos: 720
  10.                Relay_Log_File: sairl-DB-2-relay-bin.000008
  11.                 Relay_Log_Pos: 251
  12.         Relay_Master_Log_File: mysql-bin.000003
  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: 720
  25.               Relay_Log_Space: 556
  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. 1 row in set (0.00 sec)
复制代码
PS:slave_IO进程及slave_SQL进程都必须正常运行  
三、验证、测试
主从复制测试:在master数据库服务器上创建库和表,然后再插入记录,再登陆到slave服务器,看是否也建立相一致的库和表以及记录。
  1. mysql> create database test_db;
  2. Query OK, 1 row affected (0.00 sec)

  3. mysql> use test_db;
  4. Database changed
  5. mysql> create table test_table(id int(5),name char(10));
  6. Query OK, 0 rows affected (0.00 sec)

  7. mysql> insert into test_table
  8.     -> values(01,'xiujie');
  9. Query OK, 1 row affected (0.00 sec)

  10. 现在登录slave数据库服务器,看是否成功同步。
  11. mysql> show databases;
  12. +--------------------+
  13. | Database           |
  14. +--------------------+
  15. | information_schema |
  16. | mysql              |
  17. | test               |
  18. | test_db            |
  19. +--------------------+
  20. 4 rows in set (0.02 sec)

  21. mysql> use test_db;
  22. Database changed
  23. mysql> show tables;
  24. +-------------------+
  25. | Tables_in_test_db |
  26. +-------------------+
  27. | test_table        |
  28. +-------------------+
  29. 1 row in set (0.00 sec)

  30. mysql> select * from test_table;
  31. +------+--------+
  32. | id   | name   |
  33. +------+--------+
  34. |    1 | xiujie |
  35. +------+--------+
  36. 1 row in set (0.00 sec)
复制代码
OK,配置到此结束
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP