本博文出自51CTO博客王伟博主,有任何问题请进入博主页面互动讨论! |
一、部署MariaDB10.0.17
1、MariaDB下载和简单说明
当前MariaDB支持多源复制的版本为10.0的版本,最新稳定版本为10.0.17,下载连接为:http://mirrors.opencas.cn/mariadb/mariadb-10.0.17/source/mariadb-10.0.17.tar.gz。MariaDB和PerconaDB在5.5的新版中引进来线程池和关闭NUMA的概念,对数据库性能提高不少,而MySQL的版本在5.5.23(貌似是)以上的版本中也有这个概念,但是是属于企业版的功能,开源版本中没有这个功能;MariaDB10的版本中多了一个新功能就是多源复制,对于一些特殊的场景比较实用:如sharding过的表做数据汇总等,一般对汇总统计比较有用。
注:新的版本固然有很多吸引人的地方,但是其中的坑还没有挖完,有问题的话不易查找材料,不易于解决,所以不建议生产环境使用最新的版本。目前MySQL、PerconaDB和MariaDB主流版本是5.5,如果非特殊需要,5.5的足够用,抛开线程池来说,5.6的版本在整体的性能上未必比5.5的好。
2、MariaDB的安装部署
MariaDB和MySQL、Percona的基本上完全一样,5.5以后的版本采用cmake的方式编译安装:
#tar –zxfmariadb-10.0.17.tar.gz
#cdmariadb-10.0.17
#cmake .-DCMAKE_INSTALL_PREFIX=/data/percona/ -DMYSQL_DATADIR=/data/percona/data-DSYSCONFDIR=/data/percona/etc -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci-DMYSQL_UNIX_ADDR=/data/percona/tmp/mysql.sock -DENABLED_LOCAL_INFILE=ON-DENABLED_PROFILING=ON -DWITH_DEBUG=0 -DMYSQL_TCP_PORT=3306
#make&& make install
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
3、MariaDB的配置文件
这个配置文件适用于PerconaDB,去掉线程池和NAMA的参数,同样适用于MySQL:
[client]
port = 3306
socket = /data/mariadb/tmp/mysql.sock
default-character-set= utf8
[mysqld]
port = 3306
bind-address= 0.0.0.0
lower_case_table_names = 1
basedir = /data/mariadb
datadir = /data/mariadb/data
tmpdir = /data/mariadb/tmp
socket = /data/mariadb/tmp/mysql.sock
#######################################
log-output = FILE
log-error = /data/mariadb/log/error.log
#general_log
general_log_file = /data/mariadb/log/mysql.log
pid-file = /data/mariadb/data/mysql.pid
slow-query-log
slow_query_log_file = /data/mariadb/log/slow.log
tmpdir = /data/mariadb/tmp/
long_query_time = 0.1
#max_statement_time = 1000 #自动杀死超过1s的慢sql,PerconaDB5.6支持,不建议使用,如使用的和业务方沟通好,建议在特殊的情况动态配置使用,默认是0,不限制。
sync_binlog = 1
skip-external-locking
skip-name-resolve
default-storage-engine= INNODB
character-set-server= utf8
wait_timeout= 28400
back_log = 1024
#########################
thread_concurrency = 16
thread_cache_size = 512
table_open_cache = 16384
table_definition_cache = 16384
sort_buffer_size = 2M
join_buffer_size = 2M
read_buffer_size = 4M
read_rnd_buffer_size = 4M
key_buffer_size = 64M
myisam_sort_buffer_size= 64M
tmp_table_size = 256M
max_heap_table_size = 256M
open_files_limit = 65535
#####Network ######################
max_allowed_packet = 16M
interactive_timeout = 28400
wait_timeout = 28400
max-connections = 1000
max_user_connections = 0
max_connect_errors = 100
######Repl #####################
server-id = 1
report-host = 172.16.183.56
log-bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
relay-log = relay-log
#replicate-wild-do-table= zabbix.%
#replicate-wild-do-table= zabbix_server.%
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=test.%
log_slave_updates
skip-slave-start
#slave-net-timeout = 10
#rpl_semi_sync_master_enabled = 1
#rpl_semi_sync_master_wait_no_slave = 1
#rpl_semi_sync_master_timeout = 1000
#rpl_semi_sync_slave_enabled = 1
relay_log_recovery = 1
##### Innodb ###########
innodb_data_home_dir = /data/mariadb/data
innodb_data_file_path = ibdata1:2G;ibdata2:2G:autoextend
innodb_autoextend_increment = 500
innodb_log_group_home_dir = /data/mariadb/data
innodb_buffer_pool_size = 8G
innodb_buffer_pool_dump_at_shutdown= 1
innodb_buffer_pool_load_at_startup= 1
innodb_buffer_pool_instances = 8
innodb_additional_mem_pool_size= 128M
innodb_log_files_in_group = 3
innodb_log_file_size = 512M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 120
innodb_flush_method = O_DIRECT
innodb_max_dirty_pages_pct = 75
innodb_io_capacity = 1000
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 500
innodb_concurrency_tickets = 1000
innodb_open_files = 65535
innodb_file_per_table = 1
#########线程池,在高并发高负载情况下表现出出色的数据库性能 ##
thread_handling = pool-of-threads
######NUMA #########################
innodb_buffer_pool_populate = 1
##################################
[mysqldump]
quick
max_allowed_packet= 16M
[mysql]
no-auto-rehash
default-character-set=utf8
[myisamchk]
key_buffer_size= 256M
sort_buffer_size= 256M
read_buffer= 2M
write_buffer= 2M
[mysqld_safe]
######CLOSED NUMA ###########
flush_caches
numa_interleave
[mysqlhotcopy]
interactive_timeout = 28400
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
4、数据库初始化和启动
数据库初始化和启动脚本如下:
#/data/mariadb/scripts/mysql_install_db--basedir=/data/mariadb --datadir=/data/mariadb/data --defaults-file=/data/mariadb/etc/my.cnf--user=mysql
#/data/mariadb/bin/mysqld_safe--defaults-file=/data/mariadb/etc/my.cnf --user=mysql &
#echo “/data/mariadb/bin/mysqld_safe--defaults-file=/data/mariadb/etc/my.cnf --user=mysql &”>>/etc/rc.local #加入到系统启动项中
- 1.
- 2.
- 3.
- 4.
- 5.
#p#
二、MariaDB多源复制相关配置
1、初始化数据库用户
初始化多源从库的用户,建议删除初始所有用户,建立4个用户:DBA root账户,备份用户,监控用户,主从同步用户。
创建用户的相关权限和命令如下:
#创建用户
GRANT ALLPRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY IDENTIFIED BY '123456'WITH GRANT OPTION;
GRANTREPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicater'@'192.168.2.100'IDENTIFIED BY '123456';
GRANTSELECT, RELOAD, SHOW DATABASES, SUPER, LOCK TABLES, REPLICATION CLIENT, SHOWVIEW, EVENT ON *.* TO 'backup'@'localhost' IDENTIFIED BY '123456';
GRANTSELECT, PROCESS, SUPER, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO'monitor'@'127.0.0.1' IDENTIFIED BY '123456';
#删除用户建议使用dropuser xxxx@xxxxx;这样删除比较彻底。
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
作为DBA,线上的任何写操作最好做好备份,给自己留个后路
2、备份多个主库的数据库
MariaDB的多源复制,要求各个同步主库中的数据库名字各不相同,备份的话只备份需要同步的数据库即可,不需要同步的数据库可以在多源的从库中使用参数过滤掉,默认不会同步information_schema、performance_schema。
备份命令如下:
/data/mariadb/bin/mysqldump--default-character-set=utf8 --hex-blob -R --log-error=/var/log/backup-log--single-transaction --master-data=2 -uxxxx -pxxxx –B db_name > db_name_20150320.sql &
#放入后台备份
- 1.
- 2.
- 3.
3、导入备份数据
分别导入各个主库的备份数据导多源的从库中,命令如下:
/data/mariadb/bin/mysql–uxxxx –pxxxxx db_name < db_name_20150320.sql &
- 1.
4、建立主从关系
这里重点在connection_name,也就是在以前的语法上增加了connection_name,如果没加connection_name,那么默认的就是空。connection_name为标识,主要是方便用于管理单个主从关系。建立主从关系的命令如下:
Mysql>changemaster 'percona' to master_host='192.168.2.100',MASTER_PORT=3307,master_user='repl', master_password='xxxxxxx',master_log_file='mysql-bin.000019', master_log_pos=120;
其中的percona为connection_name。每个源的同步一个connection_name,分别执行上述sql命令。
- 1.
- 2.
- 3.
启动主从同步的命令为:
Mysql>START SLAVE 'percona';
- 1.
也可以在建立全部的同步关系后一起启动:
Mysql>START ALL SLAVES;
- 1.
停止单个同步的命令:
Mysql>STOP SLAVE 'percona';
- 1.
停止全部的同步的命令为:
Mysql>STOP ALL SLAVES;
- 1.
当同步建立并正常运行时,会产生relay-log,relay-log的名字为:relay-log-percona.000001,会自动的加上connection_name。
可以使用show all slaves status来查看所有的同步状态,状态信息如下:
MariaDB[(none)]> show all slaves status\G
***************************1. row ***************************
Connection_name: percona
Slave_SQL_State: Slave has readall relay log; waiting for the slave I/O thread to update it
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.2.200
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 450752689
Relay_Log_File:relay-log-percona.000011
Relay_Log_Pos: 135537605
Relay_Master_Log_File: mysql-bin.000021
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: mysql.%,test.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 450752689
Relay_Log_Space: 135537904
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Master_Server_Id: 111156
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Retried_transactions: 0
Max_relay_log_size: 1073741824
Executed_log_entries: 106216
Slave_received_heartbeats: 12
Slave_heartbeat_period: 1800.000
Gtid_Slave_Pos:
***************************2. row ***************************
Connection_name: percona
Slave_SQL_State: Slave has readall relay log; waiting for the slave I/O thread to update it
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.2.201
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 450752689
Relay_Log_File:relay-log-percona.000011
Relay_Log_Pos: 135537605
Relay_Master_Log_File: mysql-bin.000021
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: mysql.%,test.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 450752689
Relay_Log_Space: 135537904
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Master_Server_Id: 111156
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Retried_transactions: 0
Max_relay_log_size: 1073741824
Executed_log_entries: 106216
Slave_received_heartbeats: 12
Slave_heartbeat_period: 1800.000
Gtid_Slave_Pos:
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
#p#
三、MariaDB多源复制的管理
1、多源复制的管理命令
多源的复制的管理和一般的主从复制管理基本一样,常用的命令如下,具体用途一看就明白,不在赘述:
CHANGEMASTER 'connection_name';
FLUSHRELAY LOGS 'connection_name';
RESETSLAVE 'connection_name';
RESETSLAVE 'connection_name' ALL;
SHOWRELAYLOG 'connection_name' EVENTS;
SHOWSLAVE 'connection_name' STATUS;
SHOW ALLSLAVES STATUS;
STARTSLAVE 'connection_name';
START ALLSLAVES;
STOPSLAVE 'connection_name';
STOP ALLSLAVES;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
2、多源复制新添加的状态和变量
- Show global status中的新变量: Com_start_all_slaves执行 START ALL SLAVES 命令的次数。
- Com_start_slave 执行 START SLAVE 命令的次数。取代了Com_slave_start.
- Com_stop_slave 执行 STOP SLAVE 命令的次数。取代了Com_slave_stop.
- Com_stop_all_slaves 执行 STOP ALL SLAVES 命令的次数
- SHOW ALL SLAVES STATUS 有以下的新的列:
- Connection_name master的连接名。 这是第一个变量
- Slave_SQL_State SQL 线程的状态
- Retried_transactions这个连接重试事务的次数。
- Max_relay_log_size 这个连接的最大的relay日志的大小。
- Executed_log_entriesslave已经指向了多少个日志条目。
- Slave_received_heartbeats从master获得了多少个心跳。
- Slave_heartbeat_period多久从master请求一个心跳包(以秒计算)。
3、多源复制中增加的新文件
被多源复制使用的新文件的基本准则是:他们有在扩展名前被冠以连接名前缀的和原来的中继日志文件类似的名字。主要的例外是,保存所有连接名字的文件master-info-file 被简单的命名为带有 multi- 前缀的 master-info-file 。当你使用多源复制的时候,下面的文件将被创建:
- multi-master-info-file master-info-file (一般是master.info) 带上了 multi- 前缀。这里面是所有使用中的master连接信息。
- master-info-file-connection_name.extension包含了当前master应用到slave的位置。扩展名一般是 .info
- relay-log-connection_name.xxxxxrelay-log 有了一个 connection_name 的前缀. xxxxx 是 relay log 的编号。这里面保存的是从master读取的复制数据。
- relay-log-index-connection_name.extension包含可用的 relay-log-connection_name.xxxxx 文件的名字。扩展名一般是 .index
- relay-log-info-file-connection_name.extension包含relay日志中的当前master的位置。扩展名一般是 .info
当创建这些文件的时候,连接名被转化成小写的,并且其中所有的特殊字符都被转化了,就和mysql表名中的特殊字符被转化一样。这样做是为了方便文件名可以在不同系统上移植。
提示:
你只需要指定log-base-name ,所有的其他变量将会使用这个作为前缀,而不用为mysqld指定relay-log, relay-log-index,general-log, slow-log, log-bin, log-bin-index这些的名字。
其他事项
所有slave的错误信息都会加上connection name,然后被写入到error log,ER_MASTER_INFO和WARN_NO_MASTER_INFO现在会加上connection_name,这里没有冲突的解决方案,我们假设所有的master之间没有冲突。所有执行的命令都被存储在正常的binary log里面。如果你server variable log_warnings>1,那么你就会收到一些multi-master-info文件更新的信息。showslave status;看见的第一行是增加的,叫做connection_name。reset slave命令现在会删除所有的relay-log文件。
4、多源复制的典型案例和使用限制
典型的使用案例:
- 将多个master的数据整合到一个slave上,方面查询分析。
- 将多个mariadb/mysql服务器的数据整合到一个slave,方便备份。
受限的事项:
- 一个slave最多可以有64个master;
- 每个活跃的连接会创建两个线程(和mariadb复制相同);
- 你需要确认所有的master需要有不同的server-id;
- max_relay_log_size在启动后修改是不能生效的;
- innodb-recovery-update-relay-log值对默认的复制连接生效,这个参数是xtradb的特新用来存储relaylog的位置号。但是这个方案存在安全隐患,我们不推荐使用;
- Slave_net_timeout对所有参数有效,我们不检查它是否小于Slave_heartbeat_period,因为对多主复制来说没有特别意义;
- multi-source现在还不支持semisync。
5、MariaDB多源复制跳过复制错误的处理
MariaDB多源复制其中的一个复制同步失败不会影响其他的复制,那么怎么处理其中的一个复制失败那?简单的处理就是跳过失败的报错。跳过报错信息和一般的复制还是有点区别的。如其中的一个复制名字为r1,想要r1同步正常,则需要忽略即跳过该错误。
MariaDB[r1]> stop slave 'r1';
Query OK,0 rows affected (0.12 sec)
MariaDB[r1]> set @@default_master_connection='r1'; #这里是重点:指定一个通道,然后用单通道的sql_slave_skip_counter。
Query OK,0 rows affected (0.00 sec)
MariaDB[r1]> select @@default_master_connection;
+-----------------------------+
|@@default_master_connection |
+-----------------------------+
| r1 |
+-----------------------------+
1 row inset (0.00 sec)
MariaDB[r1]> SET GLOBAL sql_slave_skip_counter =1;
Query OK,0 rows affected (0.00 sec)
MariaDB[r1]> start slave 'r1';
Query OK,0 rows affected (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.