设为首页 收藏本站
查看: 1260|回复: 0

[经验分享] mysql通过gitd方式实现多线程主从复制

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-29 13:29:07 | 显示全部楼层 |阅读模式
                      
MySQL 5.6之前的版本,同步复制是单线程的,队列的,只能一个一个执行,在5.6里,可以做到多个库之间的多线程复制,例如数据库里,存放着用户表,商品表,价格表,订单表,那么将每个业务表单独放在一个库里,这时就可以做到多线程复制,但一个库里的表,多线程复制是无效的。
注,每个数据库仅能使用一个线程,复制涉及到多个数据库时多线程复制才有意义。
.环境准备
操作系统
·
CentOS 6.4 x86_64
·
软件版本
·
Mysql 5.6.13
·
1.实验拓扑
QQ截图20151029132851.png



2修改主机名
[iyunv@master ~]# uname -n
master.test.com
[iyunv@slave ~]# uname -n
Slave.test.com
3.配置名称解析
[iyunv@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
[iyunv@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



4.配置时间同步
[iyunv@master ~]# ntpdate time.nist.gov
[iyunv@slave ~]# ntpdate time.nist.gov
5关闭防火墙与SELinux
[iyunv@master ~]# service iptables stop
[iyunv@master ~]# chkconfig iptables off
[iyunv@master ~]# getenforce
Disabled
[iyunv@slave ~]# service iptables stop
[iyunv@slave ~]# chkconfig iptables off
[iyunv@slave ~]# getenforce
Disabled
6.安装并配置mysql
master:
(1).安装并链接mysql
[iyunv@master ~]# clear
[iyunv@master ~]# cd src/
[iyunv@master src]# ls
mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz
[iyunv@master src]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[iyunv@master src]# cd /usr/local/
[iyunv@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用户
[iyunv@master mysql]# groupadd -g 3306 mysql
[iyunv@master mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[iyunv@master mysql]# id mysql
uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)
(3).修改mysql安装目录所有者与所属组
[iyunv@master mysql]# chown -R root.mysql /usr/local/mysql/*
(4).初始化mysql数据库
先安装libaio库文件
[iyunv@master mysql]# yum install  -y libaio
创建数据存放目录
[iyunv@master mysql]# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"
修改目录所有者与所属组
[iyunv@master mysql]#  chown -R mysql.mysql /mydata/data/
[iyunv@master mysql]# cd /mydata/data/
初始化mysql
[iyunv@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
(5).简单修改配置文件
注,mysql5.6以后,初始化完成后会在安装目录自动生成my.cnf的配置文件,直接修改即可。
[iyunv@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提供启动脚本
[iyunv@master mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[iyunv@master mysql]# chmod +x /etc/init.d/mysqld
(7).启动并测试
[iyunv@master mysql]# service mysqld start
Starting MySQL SUCCESS!
[iyunv@master mysql]# netstat -tulnp
Proto Recv-Q Send-Q Local Address Foreign Address  State       PID/Program name  
tcp  0   0 :::3306     :::*      LISTEN      7680/mysqld

环境变量配置
[iyunv@master data]# vim /etc/profile.d/mysql.sh
exportPATH=$PATH:/usr/local/mysql/bin
[iyunv@master data]# source /etc/profile
测试一下
[iyunv@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
[iyunv@slave ~]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[iyunv@slave ~]# cd /usr/local/
[iyunv@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用户
[iyunv@slave mysql]# groupadd -g 3306 mysql
[iyunv@slave mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[iyunv@slave mysql]# id mysql
uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)
(3).修改mysql安装目录所有者与所属组
[iyunv@slave mysql]# chown -R root.mysql /usr/local/mysql/*
(4).初始化mysql数据库
安装libaio库文件
[iyunv@slave mysql]# yum install -y libaio
创建数据存放目录
[iyunv@slave mysql]# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"
修改目录所有者与所属组
[iyunv@slave mysql]#  chown -R mysql.mysql /mydata/data/
[iyunv@slave mysql]# cd /mydata/data/
初始化mysql
[iyunv@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
(5).简单修改配置文件
[iyunv@slave data]# cd /usr/local/mysql
[iyunv@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提供启动脚本
[iyunv@slave mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[iyunv@slave mysql]# chmod +x /etc/init.d/mysqld



(7).启动并测试
环境变量配置
[iyunv@slave data]# vim /etc/profile.d/mysql.sh
exportPATH=$PATH:/usr/local/mysql/bin
[iyunv@slave data]# source /etc/profile

测试登录一下
[iyunv@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
[iyunv@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
[iyunv@master mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL...... SUCCESS!
(4).查看gtid的相关信息
[iyunv@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
[iyunv@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
[iyunv@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


                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-132305-1-1.html 上篇帖子: 【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint 下篇帖子: linux下Github 同步与下载 多线程 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表