Centos7 Mysql 5.6 多主一从 解决方案与详细配置

业务场景:

公司几个主要的业务已经独立,放在不同的数据库服务器上面,但是有一个业务又需要关联多个业务库进行联合查询统计。这时候就需要将不同的业务库数据同步到一台从库进行统计。根据Mysql主从同步原理使用多从一主的方案解决。主库使用innodb引擎,从库开启多实例使用myisam引擎并将多个实例的数据同步到同一个目录,并通过flush tables 在一个实例里面访问其他实例的数据。

解决思路:

1、主数据库使用Innodb引擎,并设置sql_mode为 NO_AUTO_CREATE_USER
2、从库开启多实例,将多个主库里面的数据通过主从复制同步到同一个数据目录。从库的每个实例对应一个主库。多个实例使用同一个数据目录。
3、从库使用Myisam引擎,关闭从库默认的innodb引擎,Myisam引擎可以访问同一个数据目录里面其他实例的表。
4、从库的每个实例需要执行flush tables 才能看到其他实例表的数据变化,可以设置crontab任务计划每分钟在第一个实例刷新表,以便程序连接的默认实例能看到表的实时变化。
5、设置主库和从库的sql_mode都为NO_AUTO_CREATE_USER,只有这样主库的innodb引擎的sql同步到从库的时候才能执行成功。

方案架构图:

环境说明:

主库-1:192.168.1.1
主库-2:192.168.1.2
从库-3:192.168.1.3
从库-3:192.168.1.4
从库-3:192.168.1.5

实现步骤:(Mysql安装步骤这里不在描述)

1、主数据库配置文件,多个主库配置文件除了server-id不能一样其他都一样。

[root@masterdb01 ~]#cat /etc/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql
character-set-server = utf8mb4
default-storage-engine = InnoDB
socket = /tmp/mysql.sock
skip-name-resolv = 1
open_files_limit = 65535
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 2048
tmp-table-size  = 32M
max-heap-table-size = 32M
#query-cache-type = 0
query-cache-size = 0
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
query_cache_type=1
log-error=/data/logs/mysqld.log
slow_query_log = 1
slow_query_log_file = /data/logs/slow.log
long_query_time = 0.1
# BINARY LOGGING #
server-id = 1
log-bin     = /data/binlog/mysql-bin
log-bin-index  =/data/binlog/mysql-bin.index
expire-logs-days = 14
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 1024M
log_slave_updates
#binlog_format = row
binlog_format = MIXED  //这里使用的混合模式复制
relay_log_recovery = 1
#不需要同步的表
replicate-wild-ignore-table=mydb.sp_counter
#不需要同步的库
replicate-ignore-db = mysql,information_schema,performance_schema
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
transaction_isolation = REPEATABLE-READ
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 5734M
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_locks_unsafe_for_binlog = 0
[mysqldump]
quick
max_allowed_packet = 32M

2、从库配置文件。多个从库配置文件除了server-id不能一样其他都一样。

[root@slavedb01 ~]# cat /etc/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld_multi]
# 指定相关命令的路径
mysqld   = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
##复制主库1的数据##
[mysqld2]
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql
character-set-server = utf8mb4
#指定实例1的sock文件和pid文件
socket = /tmp/mysql.sock
pid-file=/data/mysql/mysql.pid
skip-name-resolv = 1
open_files_limit = 65535
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 2048
tmp-table-size  = 32M
max-heap-table-size = 32M
query-cache-size = 0
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
query_cache_type=1
#指定第一个实例的错误日志和慢查询日志路径
log-error=/data/logs/mysqld.log
slow_query_log = 1
slow_query_log_file = /data/logs/slow.log
long_query_time = 0.1
# BINARY LOGGING#
# 指定实例1的binlog和relaylog路径为/data/binlog目录
# 每个从库和每个实例的server_id不能一样。
server-id = 2
log-bin     = /data/binlog/mysql-bin
log-bin-index  =/data/binlog/mysql-bin.index
relay_log = /data/binlog/mysql-relay-bin
relay_log_index = /data/binlog/mysql-relay.index
master-info-file = /data/mysql/master.info
relay_log_info_file = /data/mysql/relay-log.info
read_only = 1
expire-logs-days = 14
sync_binlog = 1
#需要同步的库,如果不设置,默认同步所有库。
#replicate-do-db = xxx
#不需要同步的表
replicate-wild-ignore-table=mydb.sp_counter
#不需要同步的库
replicate-ignore-db = mysql,information_schema,performance_schema
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 1024M
log_slave_updates =1
#binlog_format = row
binlog_format = MIXED
relay_log_recovery = 1
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
#设置默认引擎为Myisam,下面这些参数一定要加上。
default-storage-engine=MyISAM
default-tmp-storage-engine=MYISAM
#关闭innodb引擎
skip-innodb
innodb = OFF
disable-innodb
#设置sql_mode模式为NO_AUTO_CREATE_USER
sql_mode = NO_AUTO_CREATE_USER
#关闭innodb引擎
loose-skip-innodb
loose-innodb-trx=0
loose-innodb-locks=0
loose-innodb-lock-waits=0
loose-innodb-cmp=0
loose-innodb-cmp-per-index=0
loose-innodb-cmp-per-index-reset=0
loose-innodb-cmp-reset=0
loose-innodb-cmpmem=0
loose-innodb-cmpmem-reset=0
loose-innodb-buffer-page=0
loose-innodb-buffer-page-lru=0
loose-innodb-buffer-pool-stats=0
loose-innodb-metrics=0
loose-innodb-ft-default-stopword=0
loose-innodb-ft-inserted=0
loose-innodb-ft-deleted=0
loose-innodb-ft-being-deleted=0
loose-innodb-ft-config=0
loose-innodb-ft-index-cache=0
loose-innodb-ft-index-table=0
loose-innodb-sys-tables=0
loose-innodb-sys-tablestats=0
loose-innodb-sys-indexes=0
loose-innodb-sys-columns=0
loose-innodb-sys-fields=0
loose-innodb-sys-foreign=0
loose-innodb-sys-foreign-cols=0

##复制主库2的数据##
[mysqld3]
port = 3307
basedir = /usr/local/mysql
datadir = /data/mysql
character-set-server = utf8mb4
#指定实例2的sock文件和pid文件
socket = /tmp/mysql3.sock
pid-file=/data/mysql/mysql3.pid
skip-name-resolv = 1
open_files_limit = 65535
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 2048
tmp-table-size  = 32M
max-heap-table-size = 32M
query-cache-size = 0
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
query_cache_type=1
log-error=/data/logs/mysqld3.log
slow_query_log = 1
slow_query_log_file = /data/logs/slow3.log
long_query_time = 0.1
# BINARY LOGGING #
# 这里一定要注意,不能把两个实例的binlog和relaylog放到同一个目录,
# 这里指定实例2的binlog日志为/data/binlog2目录
# 每个从库和每个实例的server_id不能一样。
server-id = 22
log-bin     = /data/binlog2/mysql-bin
log-bin-index  =/data/binlog2/mysql-bin.index
relay_log = /data/binlog2/mysql-relay-bin
relay_log_index = /data/binlog2/mysql-relay.index
master-info-file = /data/mysql/master3.info
relay_log_info_file = /data/mysql/relay-log3.info
read_only = 1
expire-logs-days = 14
sync_binlog = 1
#不需要复制的库
replicate-ignore-db = mysql,information_schema,performance_schema
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 1024M
log_slave_updates =1
#binlog_format = row
binlog_format = MIXED
relay_log_recovery = 1
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
#设置默认引擎为Myisam
default-storage-engine=MyISAM
default-tmp-storage-engine=MYISAM
#关闭innodb引擎
skip-innodb
innodb = OFF
disable-innodb
#设置sql_mode模式为NO_AUTO_CREATE_USER
sql_mode = NO_AUTO_CREATE_USER
#关闭innodb引擎,下面这些参数一定要加上。
loose-skip-innodb
loose-innodb-trx=0
loose-innodb-locks=0
loose-innodb-lock-waits=0
loose-innodb-cmp=0
loose-innodb-cmp-per-index=0
loose-innodb-cmp-per-index-reset=0
loose-innodb-cmp-reset=0
loose-innodb-cmpmem=0
loose-innodb-cmpmem-reset=0
loose-innodb-buffer-page=0
loose-innodb-buffer-page-lru=0
loose-innodb-buffer-pool-stats=0
loose-innodb-metrics=0
loose-innodb-ft-default-stopword=0
loose-innodb-ft-inserted=0
loose-innodb-ft-deleted=0
loose-innodb-ft-being-deleted=0
loose-innodb-ft-config=0
loose-innodb-ft-index-cache=0
loose-innodb-ft-index-table=0
loose-innodb-sys-tables=0
loose-innodb-sys-tablestats=0
loose-innodb-sys-indexes=0
loose-innodb-sys-columns=0
loose-innodb-sys-fields=0
loose-innodb-sys-foreign=0
loose-innodb-sys-foreign-cols=0
[mysqldump]
quick
max_allowed_packet = 32M
```

3、设置主库sql_mode,Mysql5.6默认需要在启动文件文件里面设置sql_mode才可以生效。

# cat /etc/init.d/mysqld
#other_args="$*"  # uncommon, but needed when called from an RPM upgrade action
      # Expected: "--skip-networking --skip-grant-tables"
      # They are not checked here, intentionally, as it is the resposibility
      # of the "spec" file author to give correct arguments only.
#将上面默认的#other_args开启后改为
other_args="--sql-mode=NO_AUTO_CREATE_USER"

4、开启主库和从库

#主库
service mysqld start
#开启从库的二个实例
/usr/local/mysql/bin/mysqld_multi start 2
/usr/local/mysql/bin/mysqld_multi start 3

5、在两台主库上面分别授权复制账号

#需要授权三个从库的ip可以同步
mysql> GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.1.3' IDENTIFIED BY 'rep123';
mysql> GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.1.4' IDENTIFIED BY 'rep123';
mysql> GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.1.5' IDENTIFIED BY 'rep123';
mysql> flush privileges;

6、在三个从库分别开启同步。

#进入第一个实例执行
$ mysql -S /tmp/mysql.sock
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.1',MASTER_USER='rep',MASTER_PASSWORD='rep123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=112;

#进入第二个实例执行
$ mysql -S /tmp/mysql3.sock
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.2',MASTER_USER='rep',MASTER_PASSWORD='rep123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=112;

7、测试数据同步

在二个主数据库分别建表和插入数据,到从库查看可以看到二个主库同步到同一个从库上面的所有数据。

8、在每台从库服务器上设置任务计划每分钟刷新第一个实例的表

# crontab -l
*/1 * * * * mysql -S /tmp/mysql.sock -e 'flush tables;'

Mysql5.6多主一从的坑

1、Mysql5.6默认的引擎是innodb默认同步的时候一定要把主和从的sql_mode模式里面的NO_ENGINE_SUBSTITUTION这个参数关闭。如果不关闭innodb同步到从库上面的sql将会找不到innodb引擎导致同步失败。

2、在mysql5.6开启多实例的时候第一次启动的时候在你数据库的安装目录里面(/usr/local/mysql/)会生成my.cnf配置文件,默认会优先读取数据库安装目录里面的配置文件。导致多实例不生效。

(0)

相关推荐

  • centos7 用rpm安装mysql详解

    最近做一个项目,需要用centos做数据服务器,用mysql数据库,就需要安装mysql数据库,之前没接触过centos,因此什么也不懂,就从网上也查了很多资料,都觉得不是最好的方法.最后结合mysql官方资料和网友的资料,最后用rpm方式安装,并总结给其他人以参考. 首先打开mysql管网,找到"yum repository"打开页面或者直接打开如下链接 http://dev.mysql.com/downloads/repo/yum/ ,找到自己需要的,点击download,下载rp

  • CentOs7.x安装Mysql的详细教程

    CentOS7的yum源中默认好像是没有MySQL的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. 安装mysql-community-release-el7-5.noarch.rpm包 $ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm 安装这个

  • centOS7安装MySQL数据库

    1.先检测系统是否自带原有版本mysql安装包,如果有要先卸载删除,不然不能成功安装和启动: # rpm -qa|grep mysql      查看有哪些安装包,如果直接跳过,说明没有安装包,记忆可以直接进入第2.3 # yum remove mysql mysql-server mysql-libs compat-mysql51    注意这个代码,卸载不干净 # rm -rf /var/lib/mysql # rm /etc/my.cnf # rpm -qa|grep mysql     

  • CentOS7.2虚拟机上安装MySQL 5.6.32的教程

    1.MySQL 5.6.32 64位安装包下载 在官网http://dev.mysql.com/downloads/mysql/5.6.html#中使用迅雷下载MySQL 5.6.32 64位安装包下载: http://pan.baidu.com/s/1pLCiAmf 2.把下载文件通过SecureFX传递到/usr/local/src/目录中(虚拟主机CentOS7.2的IP为:192.168.1.11) 3.解压文件. cd /usr/local/src/ [root@localhost s

  • 详解腾讯云CentOS7.0使用yum安装mysql及使用遇到的问题

    背景: 今天才申请了腾讯云+校园计划的1元服务器,安装了Centos7.0,在安装MySQL的时候,使用yum list | grep mysql 来查找yum源中是否有mysql,结果如下: [root@VM_47_56_centos ~]# yum list | grep mysql akonadi-mysql.x86_64 1.9.2-4.el7 base apr-util-mysql.x86_64 1.5.2-6.el7 base mysql-proxy.x86_64 0.8.5-2.e

  • mysql 5.7.17 安装配置方法图文教程(CentOS7)

    CentOS7安装mysql笔记 1 CentOS7默认安装mariadb数据库,卸载mariadb. rpm -qa|grep mariadb yum remove mariadb-libs.x86_64 2 配置YUM源 1)下载mysql源安装包 wget http://xiazai.jb51.net/201612/yuanma/mysql57YUM(jb51.net).rar 2)安装mysql源 yum localinstall mysql57-community-release-el

  • 详解Centos7 修改mysql指定用户的密码

    本文介绍了Centos7 修改mysql指定用户的密码,具体如下: 1.登陆mysql或者mariadb(两种任选其一) [root@localhost ~]# mysql -u root [root@localhost ~]# mysql -uroot -p 2.切换到存储用户名和密码的数据库 MariaDB [mysql]> use mysql;回车,会显示以下内容 Reading table information for completion of table and column na

  • 64位CentOs7源码安装mysql-5.6.35过程分享

    首先安装依赖包,避免在安装过程中出现问题 [root@bogon liuzhen]# yum -y install gcc gcc-c++ [root@bogon liuzhen]# yum -y install cmake [root@bogon liuzhen]# yum -y install ncurses-devel [root@bogon liuzhen]# yum -y install autoconf [root@bogon liuzhen]# yum -y install per

  • Linux centos7环境下MySQL安装教程

    Linux centos7环境下安装MySQL步骤的详细介绍 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 一.查找以前是否安装有mysql,使用下面命令: rpm –qa | grep -i mysql 如果显示有如下包则说明已安装mysql mysql-4.1.12-3.RHEL4.1 mysqlclient10-3.23.58-4.RHEL4.1 二.下载mysql解压包 mysql-5.7.1

  • Centos7下使用yum安装mysql数据库的详细教程(增强版)

    在之前给大家分享过这篇文章:CentOS 7.0下使用yum安装mysql的方法详解,小编觉得不够详细,今天给大家通过本文给大家做个补充,感兴趣的朋友可以参考下. 1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. 安装mysql-community-release-el7-5.noarch.rpm包 $ sudo rpm -ivh mysql-community-re

随机推荐