MySQL 5.6之前的版本,同步复制是单线程的,队列的,只能一个一个执行,在5.6里,可以做到多个库之间的多线程复制,例如数据库里,存放着用户表,商品表,价格表,订单表,那么将每个业务表单独放在一个库里,这时就可以做到多线程复制,但一个库里的表,多线程复制是无效的。
注,每个数据库仅能使用一个线程,复制涉及到多个数据库时多线程复制才有意义。
.环境准备
操作系统
・
CentOS 6.4 x86_64
・
软件版本
・
Mysql 5.6.13
・
[root@master ~]# uname -n
master.test.com
[root@slave ~]# uname -n
Slave.test.com
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.201 master.test.com master
192.168.18.202 slave.test.com slave
[root@slave ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.82 master.test.com master
10.10.10.89 slave.test.com slave
[root@master ~]# ntpdate time.nist.gov
[root@slave ~]# ntpdate time.nist.gov
[root@master ~]# service iptables stop
[root@master ~]# chkconfig iptables off
[root@master ~]# getenforce
Disabled
[root@slave ~]# service iptables stop
[root@slave ~]# chkconfig iptables off
[root@slave ~]# getenforce
Disabled
master:
(1).安装并链接mysql
[root@master ~]# clear
[root@master ~]# cd src/
[root@master src]# ls
mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz
[root@master src]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@master src]# cd /usr/local/
[root@master local]# ln -sv /usr/local/mysql-5.6.13-linux-glibc2.5-x86_64 mysql
"mysql" -> "/usr/local/mysql-5.6.13-linux-glibc2.5-x86_64"
(2).新建mysql用户
[root@master mysql]# groupadd -g 3306 mysql
[root@master mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@master mysql]# id mysql
uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)
(3).修改mysql安装目录所有者与所属组
[root@master mysql]# chown -R root.mysql /usr/local/mysql/*
(4).初始化mysql数据库
先安装libaio库文件
[root@master mysql]# yum install -y libaio
创建数据存放目录
[root@master mysql]# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"
修改目录所有者与所属组
[root@master mysql]# chown -R mysql.mysql /mydata/data/
[root@master mysql]# cd /mydata/data/
初始化mysql
[root@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
(5).简单修改配置文件
注,mysql5.6以后,初始化完成后会在安装目录自动生成my.cnf的配置文件,直接修改即可。
[root@master mysql]# vim my.cnf
#增加下面四行
datadir = /mydata/data
log-bin=master-bin
log-bin-index=master-bin.index
innodb_file_per_table = 1
(6).为mysql提供启动脚本
[root@master mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@master mysql]# chmod +x /etc/init.d/mysqld
(7).启动并测试
[root@master mysql]# service mysqld start
Starting MySQL SUCCESS!
[root@master mysql]# netstat -tulnp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 :::3306 :::* LISTEN 7680/mysqld
环境变量配置
[root@master data]# vim /etc/profile.d/mysql.sh
exportPATH=$PATH:/usr/local/mysql/bin
[root@master data]# source /etc/profile
测试一下
[root@master mysql]# mysql -h 127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.11 sec)
slave:
(1).安装并链接mysql
[root@slave ~]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@slave ~]# cd /usr/local/
[root@slave local]# ln -sv /usr/local/mysql-5.6.13-linux-glibc2.5-x86_64 mysql
"mysql" -> "/usr/local/mysql-5.6.13-linux-glibc2.5-x86_64"
(2).新建mysql用户
[root@slave mysql]# groupadd -g 3306 mysql
[root@slave mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@slave mysql]# id mysql
uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)
(3).修改mysql安装目录所有者与所属组
[root@slave mysql]# chown -R root.mysql /usr/local/mysql/*
(4).初始化mysql数据库
安装libaio库文件
[root@slave mysql]# yum install -y libaio
创建数据存放目录
[root@slave mysql]# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"
修改目录所有者与所属组
[root@slave mysql]# chown -R mysql.mysql /mydata/data/
[root@slave mysql]# cd /mydata/data/
初始化mysql
[root@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
(5).简单修改配置文件
[root@slave data]# cd /usr/local/mysql
[root@slave mysql]# vim my.cnf
datadir = /mydata/data
log-bin=master-bin
log-bin-index=master-bin.index
innodb_file_per_table = 1
(6).为mysql提供启动脚本
[root@slave mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@slave mysql]# chmod +x /etc/init.d/mysqld
(7).启动并测试
环境变量配置
[root@slave data]# vim /etc/profile.d/mysql.sh
exportPATH=$PATH:/usr/local/mysql/bin
[root@slave data]# source /etc/profile
测试登录一下
[root@slave mysql]# mysql -h 127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.12 sec)
8.配置mysql主从复制
(1).配置选项说明
要在MySQL 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项,
binlog-format:二进制日志的格式,有row、statement和mixed几种类型;需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;
log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于启动GTID及满足附属的其它需求;
master-info-repository和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;
sync-master-info:启用之可确保无信息丢失;
slave-paralles-workers:设定从服务器的SQL线程数;0表示关闭多线程复制功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;
binlog-rows-query-log-events:启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;
log-bin:启用二进制日志,这是保证复制功能的基本前提;
server-id:同一个复制拓扑中的所有服务器的id号必须惟一。
(2).配置主服务器master
[root@master mysql]# vim my.cnf
[mysqld]
port = 3306
datadir=/usr/local/mysql/data
socket = /var/lib/mysql/mysql.sock
#skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
log-bin=mysql-bin
innodb_file_per_table = 1
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id = 10
report-port=3306
report-host=10.10.10.82
(3).重新启动mysql
[root@master mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL...... SUCCESS!
(4).查看gtid的相关信息
[root@master mysql]# mysql -h127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show global variables like '%gtid%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON | #说明gtid功能已启动
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.10 sec)
(5).创建有复制权限的用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.18.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.44 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
(6).配置从服务器slave
[root@slave mysql]# vim my.cnf
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
datadir=/usr/local/mysql/data
log-bin=mysql-bin
innodb_file_per_table = 1
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id = 20
report-port=3306
report-host=10.10.10.89
(7).重新启动mysql
[root@slave mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL...... SUCCESS!
(8).在从服务器上使用主mysql上创建的账号密码登录并进行复制
mysql> change master to master_host='192.168.18.201', master_user='repluser',master_password='replpass',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.24 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)
(9).查看一下复制状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.201
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 151
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 363
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes #IO线程与SQL线程都是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: 151
Relay_Log_Space: 561
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
Master_UUID: 6b27d8b7-0e14-11e3-9eab-000c291192e4
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 1
1 row in set (0.00 sec)
(10).测试一下主从复制
master:
mysql> create database mydb;
Query OK, 1 row affected (0.05 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
slave:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
(11).查看一下复制状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.201
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 293
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 505
Relay_Master_Log_File: master-bin.000001
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: 293
Relay_Log_Space: 703
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
Master_UUID: 6b27d8b7-0e14-11e3-9eab-000c291192e4
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 6b27d8b7-0e14-11e3-9eab-000c291192e4:1
Executed_Gtid_Set: 3c8fa53-0e16-11e3-9eb8-000c29b8df6a:1-2,
6b27d8b7-0e14-11e3-9eab-000c291192e4:1
Auto_Position: 1