MySQL数据的主从复制、半同步复制和主主复制详解
一、MySQL复制概述
- 为mysql的安装提供前提环境和初始化安装mysql
- 创建数据库目录
- # mkdir /mydata/data –pv
- 创建mysq用户
- # useradd -r mysql
- 修改权限
- # chown -R mysql.mysql /mydata/data/
- 使用mysql-5.5通用二进制包安装
- 解压mysql软件包
- # tar xf mysql-5.5.28-linux2.6-i686.tar.gz-C /usr/local/
- 创建连接,为了方便查看mysql的版本等信息
- # cd /usr/local/
- #ln –sv mysql-5.5.28-linux2.6-i686.tar.gzmysql
- 修改属主属组
- # 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/rc.d/init.d/mysqld
- 添加至服务列表
- # chkconfig --add mysqld
- # chkconfig --list mysqld
- # chkconfig mysqld on
- 编辑配置文件,提供数据目录
- # vim /etc/my.cnf
- # The MySQL server 修改mysqld服务器端的内容
- log-bin=master-bin 主服务器二进制日志文件前缀名
- log-bin-index=master-bin.index 索引文件
- innodb_file_per_table= 1 开启innodb的一表一个文件的设置
- server-id = 1 必须是唯一的
- datadir =/mydata/data 数据目录路径
- 启动mysql服务
- # servicemysqld start
- 为了便于下面的测试,设置环境变量
- # vim/etc/profile.d/mysql.sh
- export PATH=$PATH:/usr/local/mysql/bin
- 执行环境变量脚本,使其立即生效
- # . /etc/profile.d/mysql.sh
为mysql的安装提供前提环境和初始化安装mysql
创建数据库目录
# mkdir /mydata/data –pv
创建mysq用户
# useradd -r mysql
修改权限
# chown -R mysql.mysql /mydata/data/
使用mysql-5.5通用二进制包安装
解压mysql软件包
# tar xf mysql-5.5.28-linux2.6-i686.tar.gz-C /usr/local/
创建连接,为了方便查看mysql的版本等信息
# cd /usr/local/
#ln –sv mysql-5.5.28-linux2.6-i686.tar.gzmysql
修改属主属组
# 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/rc.d/init.d/mysqld
添加至服务列表
# chkconfig --add mysqld
# chkconfig --list mysqld
# chkconfig mysqld on
编辑配置文件,提供数据目录
# vim /etc/my.cnf
# The MySQL server 修改mysqld服务器端的内容
log-bin=master-bin 主服务器二进制日志文件前缀名
log-bin-index=master-bin.index 索引文件
innodb_file_per_table= 1 开启innodb的一表一个文件的设置
server-id = 1 必须是唯一的
datadir =/mydata/data 数据目录路径
查看隔离级别:
mysql> select @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set (0.00 sec)
小结:如果你采用默认隔离级别REPEATABLE-READ,那么建议binlog_format=ROW。如果你是READ-COMMITTED隔离级别,binlog_format=MIXED和binlog_format=ROW效果是一样的,binlog记录的格式都是ROW,对主从复制来说是很安全的参数。
启动mysql服务
# servicemysqld start
为了便于下面的测试,设置环境变量
# vim/etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
执行环境变量脚本,使其立即生效
# . /etc/profile.d/mysql.sh
- 建立用户账户
- mysql> grant replication slave on *.* to 'chris'@'172.16.%.%' identified by 'work';
- 刷新数据使其生效
- mysql> flush privileges;
建立用户账户
mysql> grant replication slave on *.* to 'chris'@'172.16.%.%' identified by 'work';
刷新数据使其生效
mysql> flush privileges;
- 创建mysql数据库目录
- # mkdir /mydata/data –pv
- 创建mysql用户
- # useradd -r mysql
- 修改数据目录权限
- # chown -R mysql.mysql /mydata/data/
- 使用mysql-5.5通用二进制包安装mysql
- 解压mysql软件包
- # tar xf mysql-5.5.28-linux2.6-i686.tar.gz-C /usr/local/
- 创建连接,便于查看mysql的版本等信息
- # cd /usr/local/
- # ln –sv mysql-5.5.28-linux2.6-i686.tar.gzmysql
- 修改mysql属主属组
- # cd mysql
- # chown -R root.mysql ./*
- 初始化mysql数据库
- # scripts/mysql_install_db –user=mysql--datadir=/mydata/data/
- 提供mysql配置文件
- # cp support-files/my-large.cnf /etc/my.cnf
- 提供服务脚本
- # cp support-files/mysql.server /etc/init.d/mysqld
- 添加至服务列表
- # chkconfig --add mysqld
- 编辑配置文件
- # vim /etc/my.cnf
- # The MySQL server
- #log-bin=mysql-bin 禁用二进制日志,从服务器不需要二进制日志文件
- datadir = /mydata/data mysql的数据目录
- relay-log = relay-log 设置中继日志
- relay-log-index = relay-log.index 中继日志索引
- innodb_file_per_table = 1
- server-id = 2 id不要和主服务器的一样
- 设置环境变量
- # vim/etc/profile.d/mysql.sh
- export PATH=$PATH:/usr/local/mysql/bin
- 执行此脚本(导出环境变量)
- # . /etc/profile.d/mysql.sh
- 启动服务
- # service mysqld start
创建mysql数据库目录
# mkdir /mydata/data –pv
创建mysql用户
# useradd -r mysql
修改数据目录权限
# chown -R mysql.mysql /mydata/data/
使用mysql-5.5通用二进制包安装mysql
解压mysql软件包
# tar xf mysql-5.5.28-linux2.6-i686.tar.gz-C /usr/local/
创建连接,便于查看mysql的版本等信息
# cd /usr/local/
# ln –sv mysql-5.5.28-linux2.6-i686.tar.gzmysql
修改mysql属主属组
# cd mysql
# chown -R root.mysql ./*
初始化mysql数据库
# scripts/mysql_install_db –user=mysql--datadir=/mydata/data/
提供mysql配置文件
# cp support-files/my-large.cnf /etc/my.cnf
提供服务脚本
# cp support-files/mysql.server /etc/init.d/mysqld
添加至服务列表
# chkconfig --add mysqld
编辑配置文件
# vim /etc/my.cnf
# The MySQL server
#log-bin=mysql-bin 禁用二进制日志,从服务器不需要二进制日志文件
datadir = /mydata/data mysql的数据目录
relay-log = relay-log 设置中继日志
relay-log-index = relay-log.index 中继日志索引
innodb_file_per_table = 1
server-id = 2 id不要和主服务器的一样
设置环境变量
# vim/etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
执行此脚本(导出环境变量)
# . /etc/profile.d/mysql.sh
启动服务
# service mysqld start
- mysql> show master status; #在Master上执行查看二进制文件
- 在从服务器上开启复制功能
- change master to master_host='172.16.7.1',master_user='chris',master_password='work',master_log_file='master-bin.000001',master_log_pos=407;
- 开启复制功能
- mysql>start slave;
mysql> show master status; #在Master上执行查看二进制文件
在从服务器上开启复制功能
change master to master_host='172.16.7.1',master_user='chris',master_password='work',master_log_file='master-bin.000001',master_log_pos=407;
开启复制功能
mysql>start slave;
![图像 16.png](https://i-blog.csdnimg.cn/blog_migrate/e46d056a69b286531e3ad3ba3e49c0bf.png)
- 安装插件:mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
- 启动模块:mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
- 设置超时时间:mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
安装插件:mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
启动模块:mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
设置超时时间:mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
1
2
3
4
|
slave:
安装插件:msyql> INSTALL PLUGIN rpl_semi_sync_slave SONAME
'semisync_slave.so'
;
启动模块:mysql> SET GLOBAL rpl_semi_sync_slave_enabled =
1
;
重启进程使其模块生效:mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
|
- 在Master和Slave的my.cnf中编辑:
- # On Master
- [mysqld]
- rpl_semi_sync_master_enabled=1
- rpl_semi_sync_master_timeout=1000 #此单位是毫秒
- # On Slave
- [mysqld]
- rpl_semi_sync_slave_enabled=1
在Master和Slave的my.cnf中编辑:
# On Master
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 #此单位是毫秒
# On Slave
[mysqld]
rpl_semi_sync_slave_enabled=1
- master:
- mysql> CREATE DATABASE asyncdb;
- master> SHOW STATUS LIKE 'Rpl_semi_sync_master_yes_tx';
- slave> SHOW DATABASES;
- 其测试过程如下
master:
mysql> CREATE DATABASE asyncdb;
master> SHOW STATUS LIKE 'Rpl_semi_sync_master_yes_tx';
slave> SHOW DATABASES;
其测试过程如下
![图像 25.png](https://i-blog.csdnimg.cn/blog_migrate/6494f916fdc1a43bb82930fb930d5baf.png)
- # 主服务器上
- [mysqld]
- server-id = 1
- log-bin = mysql-bin
- relay-log = relay-mysql
- relay-log-index = relay-mysql.index
- auto-increment-increment = 2 #每次跳两个数。
- auto-increment-offset = 1 #从1开始。
# 主服务器上
[mysqld]
server-id = 1
log-bin = mysql-bin
relay-log = relay-mysql
relay-log-index = relay-mysql.index
auto-increment-increment = 2 #每次跳两个数。
auto-increment-offset = 1 #从1开始。
- [mysqld]
- server-id = 2
- log-bin = mysql-bin
- relay-log = relay-mysql
- relay-log-index = relay-mysql.index
- auto-increment-increment = 2
- auto-increment-offset = 2
[mysqld]
server-id = 2
log-bin = mysql-bin
relay-log = relay-mysql
relay-log-index = relay-mysql.index
auto-increment-increment = 2
auto-increment-offset = 2
- master:查看日志文件信息
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000001 | 107 | | |
- +------------------+----------+--------------+------------------+
- Slave:查看服务器日志文件信息
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000001 | 107 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
master:查看日志文件信息
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | | |
+------------------+----------+--------------+------------------+
Slave:查看服务器日志文件信息
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
1
2
3
|
master:
mysql> GRANT REPLICATION SLAVE ON *.* TO
'chrislee'
@
'172.16.%.%'
IDENTIFIED BY
'work'
;
mysql> flush privileges;
|
- slave:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'chrisli'@'172.16.%.%' IDENTIFIED BY 'work';
- mysql> flush privileges
slave:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'chrisli'@'172.16.%.%' IDENTIFIED BY 'work';
mysql> flush privileges
- server1
- mysql> CHANGE MASTER TO MASTER_HOST='172.16.7.2',MASTER_USER='chrisli',MASTER_PASSWORD='work',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=344;
server1
mysql> CHANGE MASTER TO MASTER_HOST='172.16.7.2',MASTER_USER='chrisli',MASTER_PASSWORD='work',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=344;
- server2:
- mysql> CHANGE MASTER TO MASTER_HOST='172.16.7.1',MASTER_USER='chrislee',MASTER_PASSWORD='work',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=345;
server2:
mysql> CHANGE MASTER TO MASTER_HOST='172.16.7.1',MASTER_USER='chrislee',MASTER_PASSWORD='work',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=345;