mysql主从复制

mysql主从复制

时间:2013-12-01 02:12:00      作者:beebol      标签: master mysql replication slave      分类: Mysql

环境说明:

一台master:

IP地址:192.168.0.105                     mysql版本:5.5.15

一台slave:

IP地址:192.168.0.104                     mysql版本:5.5.15

首先来配置master,修改/etc/my.cnf文件,在[mysqld]下增加如下内容:

log-bin=mysql-bin 
#开启二进制日志,这个是必须的,mysql主从复制就是基于二进制日志的
server-id       = 1#主从必须有一个唯一的server-id
#主从必须有一个唯一的server-id

master配置很简单,配置好后重启mysql,给复制添加专门的用户。

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT on *.* to beebol@'192.168.0.104' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR beebol@'192.168.0.104'; 
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for beebol@192.168.0.104                                                                                                                   |
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'beebol'@'192.168.0.104' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+---------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.01 sec)
1 row in set (0.01 sec)

只要给从mysql  replication slave和replication client就可以了。

然后来配置slave端,修改/etc/my.cnf

#log-bin=mysql-bin   
#在从上就不需要开启二进制日志了
relay-log=relay-bin
relay-log-index=relay-bin.index
#开启中继日志
# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id   = 11
#这个server-id很关键,不能与上面的主server-id一致就行
replicate-do-db=beebolblog#只复制beebolblog库
#只复制beebolblog库

再将master中的beebolblog数据库备份导出来,导入到从库

mysqldump -f --single-transaction -R -q --master-data=1 -B beebolblog >beebolblog.sql      
[root@localhost ~]# scp beebolblog.sql 192.168.0.104:/tmpbeebolblog.sql                                                                                    100% 2336KB   2.3MB/s   00:00
beebolblog.sql                                                                                    100% 2336KB   2.3MB/s   00:00

把master备份库beebolblog数据库导入到slave中:

[root@server1 tmp]# mysql <beebolblog.sql 
[root@server1 tmp]# grep 'CHANGE MASTER' beebolblog.sql 
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000015', MASTER_LOG_POS=107;

#导入后,再看看从服务器应该从那个二进制日志开始,什么位置开始。显示这个change master信息中在导出数据时添加了--master-data

#在slave端进行配置连接信息
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.105',MASTER_USER='beebol',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000015',MASTER_LOG_POS=107;
Query OK, 0 rows affected (0.08 sec)

#配置好后,可以启动slave
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

#查看状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.105
                  Master_User: beebol
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000015
          Read_Master_Log_Pos: 355
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 501
        Relay_Master_Log_File: mysql-bin.000015
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: beebolblog
          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: 355
              Relay_Log_Space: 651
              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: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified
No query specified

查看状态显示两个线程正常:Slave_IO_Running: Yes,Slave_SQL_Running: Yes,这表示一切正常。

Slave_IO_Running:IO线程负责接收master传过来的二进制日志,放入中继日志当中

Slave_SQL_Running: SQL线程负责将中继日志的sql语句执行一遍

如上操作就可以实现mysql的主从复制功能了。

 

时间:2013-12-01 02:12:00      作者:beebol      标签: master mysql replication slave      分类: Mysql
  • 分享到:
  • 微博
  • QQ空间
  • 腾讯微博
  • 微信

Copyright © 2015 Gitblog | Proudly powered by Gitblog.