MYSQL的主从和主主复制模式

一、复制介绍

    MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器的日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。

    请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。

    单向复制有利于健壮性、速度和系统管理:

    •主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作备份。

    •通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。SELECT查询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。

    •使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。

MySQL提供了数据库的同步功能,这对我们事先数据库的容灾、备份、恢复、负载均衡等都是有极大帮助的。

二、实验环境

    操作系统:RHEL 5.4 X86

    mysql:5.5.22版本

    master机器名:node2      IP:192.168.1.152

    slave机器名:node1         IP:192.168.1.151

三、MySQL主从模式

    注意Mysql数据库的版本,两个数据库版本要相同,或者slave比master版本低!

    3.1、通过源码安装mysql

    此处过程略,可以参考<Mysql 源码安装>!

    3.2、Master端:

    3.2.1、创建目录

1 [root@node2 ~]# mkdir -p /var/log/mysql         
2 [root@node2 ~]# chown -R mysql:mysql /var/log/mysql   //创建更新目录并赋予mysql用户权限

    3.2.2、修改配置文件:

1 [root@node2 mysql]# vi my.cnf               //编辑配置文件增加以下内容 
2 log-bin=mysql-bin                                  //启动二进制日志系统 
3 binlog-do-db=node1
4 binlog-do-db=node2                                //二进制需要同步的数据库名 如果有多个数据库,每个数据库一行
5 server-id = 1                                    //本机数据库ID
6 log-bin=/var/log/mysql/updatelog          //设定生成log文件名,这里的路径没有mysql目录要手动创建并给于它mysql用户的权限
7 binlog-ignore-db=mysql                   //避免同步mysql用户配置,以免不必要的麻烦

    3.2.3、创建用于同步的用户:

1 mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO 'replication'@'192.168.1.151' IDENTIFIED BY '123456'; 
2 Query OK, 0 rows affected (0.00 sec)      //给从服务器用户replication 的同步权限   
3 
4 mysql> Grant ALL PRIVILEGES ON node1.* TO replication@'%' IDENTIFIED BY '123456';           //创建用户replication,密码123456,允许所有用户访问数据库node1,并刷新权限 
5 Query OK, 0 rows affected (0.00 sec)    
6 
7 mysql> Grant ALL PRIVILEGES ON node2.* TO replication@'%' IDENTIFIED BY '123456';  
8 Query OK, 0 rows affected (0.00 sec)

    3.2.4、复制数据到从库:

 1 mysql> create database node1;
 2 Query OK, 1 row affected (0.00 sec)
 3 
 4 mysql> create database node2;
 5 Query OK, 1 row affected (0.00 sec)
 6 
 7 mysql> use node1;
 8 Database changed
 9 mysql> create table node1 (id char) engine=myisam;
10 Query OK, 0 rows affected (0.01 sec)
11 
12 mysql> insert into node1 values(1);
13 Query OK, 1 row affected (0.00 sec)
14 
15 mysql> commit;
16 Query OK, 0 rows affected (0.00 sec)
17 
18 mysql> exit;
19 Bye
20 [root@node2 app]# service mysql stop;
21 Shutting down MySQL...                                     [  OK  ]
22 
23 [root@node2 data]# tar -cvf db.tar node1 node2
24 node1/
25 node1/node1.MYI
26 node1/db.opt
27 node1/node1.frm
28 node1/node1.MYD
29 node2/
30 node2/db.opt
31 [root@node2 data]# ll
32 total 30140
33 -rw-r--r-- 1 root  root     20480 Nov 13 19:29 db.tar
34 
35 [root@node2 data]# scp db.tar node1:/app/mysql/data/
36 db.tar                                                                 100%   20KB  20.0KB/s   00:00    
37 
38 [root@node2 data]# service mysql start
39 Starting MySQL..                                           [  OK  ]
40 
41 [root@node2 data]# mysql
42 Welcome to the MySQL monitor.  Commands end with ; or g.
43 Your MySQL connection id is 1
44 Server version: 5.5.22-log Source distribution
45 
46 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
47 
48 Oracle is a registered trademark of Oracle Corporation and/or its
49 affiliates. Other names may be trademarks of their respective
50 owners.
51 
52 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
53 
54 mysql> show master status;
55 +------------------+----------+--------------+------------------+
56 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
57 +------------------+----------+--------------+------------------+
58 | updatelog.000001 |      107 | node1,node2  | mysql            |
59 +------------------+----------+--------------+------------------+
60 1 row in set (0.00 sec)

    3.3、Slave端:

    3.3.1、修改配置文件:

 1 [root@node1 data]# vi ../my.cnf
 2 server-id       = 2                           //从服务器ID号  
 3 #master_host=192.168.1.152        //主服务器地址 
 4 #master_user=replication              //指定在主服务器上可以进行同步的用户名   
 5 #master_password=123456           //密码  
 6 #master-port=3306                        //端口号   
 7 #master-connect-retry=60               //断点重连时间       
 8 replicate-ignore-db=mysql              //屏蔽对mysql库的同步,以免有麻烦   
 9 replicate-do-db=node1                         //同步数据库名称  
10 replicate-do-db=node2
11 ----由于5.3以后不支持master_host参数等,因此下面采用change master to的方式

    3.3.2、装载主服务器数据库:

[root@node1 data]# tar -xvf db.tar
node1/
node1/node1.MYI
node1/db.opt
node1/node1.frm
node1/node1.MYD
node2/
node2/db.opt
[root@node1 data]# chown -R mysql:mysql node1
[root@node1 data]# chown -R mysql:mysql node2
[root@node1 data]# service mysql restart
Starting MySQL                                             [  OK  ]

    3.3.3、同步数据:

mysql> slave stop
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host='192.168.1.152', master_user='replication', master_password='123456';
Query OK, 0 rows affected (0.01 sec)

mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.152
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: updatelog.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: node1-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: updatelog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: node1,node2
          Replicate_Ignore_DB: mysql
           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: 107
              Relay_Log_Space: 409
              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

mysql> use node1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_node1 |
+-----------------+
| node1           |
+-----------------+
1 row in set (0.00 sec)

mysql> select * from node1;
+------+
| id   |
+------+
| 1    |
+------+
1 row in set (0.00 sec)

    3.4、测试数据

----主库端操作
mysql> use node1;
Database changed
mysql> insert into node1 values(2);
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> use node2;
Database changed
mysql> create table node2 (id char);
Query OK, 0 rows affected (0.01 sec)

----备库端查询
mysql> select * from node1;
+------+
| id   |
+------+
| 1    |
| 2    |
+------+
2 rows in set (0.00 sec)

mysql> use node2;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_node2 |
+-----------------+
| node2           |
+-----------------+
1 row in set (0.00 sec)

 四、MySQL主主模式:

    思路,互为对方的从服务器,每台服务器即是对方的主服务器,又是对方的从服务器。

    在这里就省略了!

原文地址:https://www.cnblogs.com/Richardzhu/p/3422840.html