Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9268315
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: Mysql/postgreSQL

2013-11-27 15:11:42

mysql 添加第N个从库
2011-12-15 17:20:47
标签:linux mysql binlog 主从 从库
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
某项目mysql服务器 1主 1从 ,现在要添加一个mysql从服务器,要求主库不能停止服务,以前由于不是线上的服务器,可以在主服务器上 执行 flush tables with read lock 语句(锁表,只读),所有的表只能读不能写,然后再拷贝主库数据到新的从库服务器上,并保持数据一致性,现在只能换一种方法了,思路 新从库2拷贝老的从库1的数据!




老从库1 相关操作:


#1 停止 mysql从库,锁表,
     记住 Read_Master_Log_Pos: 与 Master_Log_File: (红色字)
mysql> stop slave; 
mysql> flush tables with read lock; 
mysql> show slave status\G; 
*************************** 1. row *************************** 
               Slave_IO_State: 
                  Master_Host: 192.168.6.53 
                  Master_User: dongnan 
                  Master_Port: 3306 
                Connect_Retry: 60 
              Master_Log_File: mysql-bin.000097 
          Read_Master_Log_Pos: 19157395 
               Relay_Log_File: zabbix-slave-relay-bin.000185 
                Relay_Log_Pos: 11573578 
        Relay_Master_Log_File: mysql-bin.000097 
             Slave_IO_Running: No 
            Slave_SQL_Running: No 
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0 
                   Last_Error: 
                 Skip_Counter: 0 
          Exec_Master_Log_Pos: 19157395 
              Relay_Log_Space: 19142103 
              Until_Condition: None 
               Until_Log_File: 
                Until_Log_Pos: 0 
           Master_SSL_Allowed: No 
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL 
Master_SSL_Verify_Server_Cert: No 
                Last_IO_Errno: 0 
                Last_IO_Error: 
               Last_SQL_Errno: 0 
               Last_SQL_Error: 
1 row in set (0.00 sec) 
 
ERROR: 
No query specified 


#2 打包数据并发送到新从库2服务器上
cd /usr/local/mysql/var/ 
tar czvf zabbix_20110805.tar.gz zabbix/ 
scp zabbix_20110805.tar.gz root@192.168.6.54:/root 




新从库2相关操作:
#1 更改 server-id 值不能为1,因为master 的 server-id=1 
vim /etc/my.cnf  
server-id = 3 
#2 停止mysql数据库并解压缩到/usr/local/mysql/var/  
tar zxvf zabbix_20110805.tar.gz -C /usr/local/mysql/var/
#3 启动mysql数据库并change master
Read_Master_Log_Pos 值  19157395 
Master_Log_File 值 mysql-bin.000097
 
mysql> change master to master_host='192.168.6.53',master_user='dongnan',master_password='password',master_log_file='mysql-bin.000097',master_log_pos=19157395; 
 
mysql> slave start;                    #启动slave
 
mysql> show slave status\G;            #显示slave 状态
*************************** 1. row *************************** 
               Slave_IO_State: Waiting for master to send event 
                  Master_Host: 192.168.6.53 
                  Master_User: dongnan 
                  Master_Port: 3306 
                Connect_Retry: 60 
              Master_Log_File: mysql-bin.000097 
          Read_Master_Log_Pos: 21194359 
               Relay_Log_File: db1-relay-bin.000002 
                Relay_Log_Pos: 2037215 
        Relay_Master_Log_File: mysql-bin.000097 
             Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes 
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0 
                   Last_Error: 
                 Skip_Counter: 0 
          Exec_Master_Log_Pos: 21194359 
              Relay_Log_Space: 2037368 
              Until_Condition: None 
               Until_Log_File: 
                Until_Log_Pos: 0 
           Master_SSL_Allowed: No 
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0 
Master_SSL_Verify_Server_Cert: No 
                Last_IO_Errno: 0 
                Last_IO_Error: 
               Last_SQL_Errno: 0 
               Last_SQL_Error: 
1 row in set (0.00 sec) 
 
ERROR: 
No query specified 




验证重库是否同步:


老从库1
mysql> slave start; 
 
mysql> show slave status\G; 
*************************** 1. row *************************** 
               Slave_IO_State: Waiting for master to send event 
                  Master_Host: 192.168.6.53 
                  Master_User: dongnan 
                  Master_Port: 3306 
                Connect_Retry: 60 
              Master_Log_File: mysql-bin.000097 
          Read_Master_Log_Pos: 21194359 
               Relay_Log_File: db1-relay-bin.000002 
                Relay_Log_Pos: 2037215 
        Relay_Master_Log_File: mysql-bin.000097 
             Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes 




新从库2
mysql> show slave status\G; 
*************************** 1. row *************************** 
               Slave_IO_State: Waiting for master to send event 
                  Master_Host: 192.168.6.53 
                  Master_User: dongnan 
                  Master_Port: 3306 
                Connect_Retry: 60 
              Master_Log_File: mysql-bin.000097 
          Read_Master_Log_Pos: 21194359 
               Relay_Log_File: db1-relay-bin.000002 
                Relay_Log_Pos: 2037215 
        Relay_Master_Log_File: mysql-bin.000097 
             Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes 
 
结束
既然主库不能动,那就去操作从库吧,新从库2拷贝老的从库1的数据!
本文出自 “dongnan” 博客,转载请与作者联系!
阅读(955) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~