MariaDB10.0實例部署和多源複製配置

MariaDB10.0實例部署和多源複製配置

一、部署MariaDB10.0.17

1MariaDB下載和簡單說明

當前MariaDB支持多源複製的版本爲10.0的版本,最新穩定版本爲10.0.17,下載連接爲:http://mirrors.opencas.cn/mariadb/mariadb-10.0.17/source/mariadb-10.0.17.tar.gzMariaDBPerconaDB5.5的新版中引進來線程池和關閉NUMA的概念,對數據庫性能提高不少,而MySQL的版本在5.5.23(貌似是)以上的版本中也有這個概念,但是是屬於企業版的功能,開源版本中沒有這個功能;MariaDB10的版本中多了一個新功能就是多源複製,對於一些特殊的場景比較實用:如sharding過的表做數據彙總等,一般對彙總統計比較有用。

注:新的版本固然有很多吸引人的地方,但是其中的坑還沒有挖完,有問題的話不易查找材料,不易於解決,所以不建議生產環境使用最新的版本。目前MySQLPerconaDBMariaDB主流版本是5.5,如果非特殊需要,5.5的足夠用,拋開線程池來說,5.6的版本在整體的性能上未必比5.5的好。

2MariaDB的安裝部署

MariaDBMySQLPercona的基本上完全一樣,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

3MariaDB的配置文件

這個配置文件適用於PerconaDB,去掉線程池和NAMA的參數,同樣適用於MySQL

[client]

port            = 3306

socket          = /data/mariadb/tmp/mysql.sock


[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的慢sqlPerconaDB5.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

 prompt = "MySQL \u@[\d]>"


[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

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  #加入到系統啓動項中

二、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;這樣刪除比較徹底。

作爲DBA,線上的任何寫操作最好做好備份,給自己留個後路

2、備份多個主庫的數據庫

MariaDB的多源複製,要求各個同步主庫中的數據庫名字各不相同,備份的話只備份需要同步的數據庫即可,不需要同步的數據庫可以在多源的從庫中使用參數過濾掉,默認不會同步information_schemaperformance_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 &

#放入後臺備份

3、導入備份數據

分別導入各個主庫的備份數據導多源的從庫中,命令如下:

/data/mariadb/bin/mysql–uxxxx –pxxxxx db_name < db_name_20150320.sql &

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;

其中的perconaconnection_name。每個源的同步一個connection_name,分別執行上述sql命令。

啓動主從同步的命令爲:

Mysql>START SLAVE 'percona';

也可以在建立全部的同步關係後一起啓動:

Mysql>START ALL SLAVES;

停止單個同步的命令:

Mysql>STOP SLAVE 'percona';

停止全部的同步的命令爲:

Mysql>STOP ALL SLAVES;

當同步建立並正常運行時,會產生relay-logrelay-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:

三、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;

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_heartbeatsmaster獲得了多少個心跳。

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 logER_MASTER_INFOWARN_NO_MASTER_INFO現在會加上connection_name,這裏沒有衝突的解決方案,我們假設所有的master之間沒有衝突。所有執行的命令都被存儲在正常的binary log裏面。如果你server variable log_warnings>1,那麼你就會收到一些multi-master-info文件更新的信息。showslave status;看見的第一行是增加的,叫做connection_namereset slave命令現在會刪除所有的relay-log文件。

4、多源複製的典型案例和使用限制

典型的使用案例:

將多個master的數據整合到一個slave,方面查詢分析。

將多個mariadb/mysql服務器的數據整合到一個slave,方便備份。

受限的事項:

一個slave最多可以有64master;

每個活躍的連接會創建兩個線程(mariadb複製相同);

你需要確認所有的master需要有不同的server-id;

max_relay_log_size在啓動後修改是不能生效的;

innodb-recovery-update-relay-log值對默認的複製連接生效,這個參數是xtradb的特新用來存儲relaylog的位置號。但是這個方案存在安全隱患,我們不推薦使用;

Slave_net_timeout對所有參數有效,我們不檢查它是否小於Slave_heartbeat_period,因爲對多主複製來說沒有特別意義;

multi-source現在還不支持semisync

5MariaDB多源複製跳過複製錯誤的處理

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)

 

MariaDB[r1]> show slave 'r1' status\G;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章