mysql数据库主从及主主复制配置演示

8/2/2015来源:MySQL教程人气:1915

实验系统:CentOS 6.6_x86_64

实验前提:提前准备好编译环境,防火墙和selinux都关闭

实验说明:本实验共有2台主机,ip分配如拓扑

实验软件:mariadb-10.0.20

实验拓扑:

    

一、安装MySQL

  1.编译安装mariadb:

tar xf mariadb-10.0.20-linux-x86_64.tar.gz  -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
mkdir -pv /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

  2.编辑配置文件:

vim /etc/my.cnf
--------------------------------
datadir = /mydata/data             //增加此行
--------------------------------
service mysqld start
/usr/local/mysql/bin/mysql        //连接测试

    

二、主从复制

  实验中我先让19.43当主服务器,19.48为从服务器。

  1.主服务器操作:

    (1)编辑my.cnf,启用二进制日志(如果定义到其他路径,请给予其mysql权限):

log-bin=/mydata/data/mysql-bin

    (2)定义server-id:

server-id       = 1

    (3)创建有复制权限的账号:

/usr/local/mysql/bin/mysql
-----------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO slave@192.168.19.48 IDENTIFIED BY '123456';      //遵循最小权限原则
FLUSH PRIVILEGES;

    (4)记录最后的二进制日志信息,CHANGE MASTER时会用到:

SHOW MASTER LOGS;

    

  2.从服务器操作:

    (1)启动中继日志(如果定义到其他路径,请给予其mysql权限):

relay_log=/mydata/data/relay-log

    (2)从服务器用中继日志就足够了,关闭二进制日志,减少磁盘IO:

#log-bin=mysql-bin             //将其注释
#binlog_format=mixed

    (3)定义server-id:

server-id       = 2            //不能与主服务器相同

    (4)配置CHANGE MASTER:

/usr/local/mysql/bin/mysql
-----------------------------------------
CHANGE MASTER TO MASTER_HOST='192.168.19.43',MASTER_USER='slave',MASTER_PASSWord='123456',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=326;
SHOW SLAVE STATUS\G //查看状态
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.19.43
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 326
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             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: 326
              Relay_Log_Space: 248
              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: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
1 row in set (0.00 sec)

    (5)启动io thread以及sql thread:

START SLAVE;

    

  3.在主服务器创建数据库,从服务器查看:

CREATE DATABASE jason;     //主服务器创建数据库
SHOW DATABASES; //从服务器查看

    

  4.如果主数据库不是新建立的,而是使用过一段时间,且里面已经有不少数据的情况下,需要先把主服务器数据导出,再导入到从服务器,然后根据上面的步骤进行主从复制,这里将不再演示。

  导出数据库命令参考:mysqldump --all-databases --lock-all-tables --master-data=2 > name.sql

三、半同步复制

  1.什么是半同步复制?

    因为开启这个功能之后,主服务器只等待多个从服务器中的指定的一台从服务器复制成功,然后才进行其他写操作,使这个从服务器和主服务器上的数据完全同步,而并不管其他的从服务器。这在一定程度上就保证了我们数据的安全性。这个功能需要打由google贡献的补丁,它是以插件的形式存在的。补丁存放的位置在:/usr/local/mysql/lib/plugin/

  2.主服务器打补丁:

/usr/local/mysql/bin/mysql
---------------------------------------------
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SHOW GLOBAL VARIABLES LIKE '%semi%';        //查看状态

    

  3.主服务器修改配置:

SET GLOBAL rpl_semi_sync_master_enabled=1;
SET GLOBAL rpl_semi_sync_master_timeout=1000;

  4.从服务器打补丁:

/usr/local/mysql/bin/mysql
---------------------------------------------
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SHOW GLOBAL VARIABLES LIKE '%semi%';

    

  5.从服务器修改配置:

SET GLOBAL rpl_semi_sync_slave_enabled=1;
STOP SLAVE;
START SLAVE;

  6.主服务器查看状态:

SHOW GLOBAL STATUS LIKE '%semi%';

    

四、主主复制

  1.由于刚才做了许多操作,为了不影响之后的实验,我们先将数据库进行重置:

service mysqld stop
rm -rf /mydata/data/*                                                                     //删除数据库文件
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/mydata/data/        //初始化mysql

  2.修改my.cnf:

    19.43上:

vim /etc/my.cnf
-------------------------------------------
[mysqld]
server-id=1
log-bin=/mydata/data/mysql1-bin               //开启二进制日志
relay_log=/mydata/data/relay-log              //开始中继日志
auto-increment-increment = 2                  //每次增长的数值
auto-increment-offset = 1                     //开始的数值

    19.48上:

vim /etc/my.cnf
-------------------------------------------
[mysqld]
server-id=2
log-bin=/mydata/data/mysql2-bin
relay_log=/mydata/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 2

  3.创建具有复制权限的用户:

    19.43上:

service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO mmm@192.168.19.48 IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

    19.48上:

service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO mmm@192.168.19.43 IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

  4.查看二进制位置:

    19.43上:

SHOW MASTER LOGS;

    

    19.48上使用同样的命令:

    

  5.配置CHANGE MASTER:

    19.43上:

CHANGE MASTER TO MASTER_HOST='192.168.19.48',MASTER_USER='mmm',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql2-bin.000001',MASTER_LOG_POS=642;
START SLAVE;

    19.48上:

CHANGE MASTER TO MASTER_HOST='192.168.19.43',MASTER_USER='mmm',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql1-bin.000001',MASTER_LOG_POS=642;
START SLAVE;

  6.查看状态:

SHOW SLAVE STATUS\G

     

    

  之后的复制演示这里不再演示,请大家自行尝试。至此,所有试验演示完毕,谢谢!