mariadb-5.5.52主從複製集群安裝步驟
在港閘等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都營(yíng)銷網(wǎng)站建設(shè),外貿(mào)營(yíng)銷網(wǎng)站建設(shè),港閘網(wǎng)站建設(shè)費(fèi)用合理。
一.因?yàn)榫W(wǎng)站業(yè)務(wù)對(duì)于數(shù)據(jù)讀取一致性要求非常高,所以mariadb數(shù)據(jù)庫就必須要用主從復(fù)制集群的形式來滿足業(yè)務(wù)需求
二.復(fù)制配置過程簡(jiǎn)介
有兩臺(tái)Mariadb數(shù)據(jù)庫服務(wù)器db01和db02,db01為主服務(wù)器,db02為從服務(wù)器,初始狀態(tài)時(shí),db01和db02中的數(shù)據(jù)信息完全相同,當(dāng)db01中的數(shù)據(jù)發(fā)生變化時(shí),db02也會(huì)跟著發(fā)生相應(yīng)的變化,使得db01和db02的數(shù)據(jù)信息同步,從而達(dá)到備份的目的。
要點(diǎn):
負(fù)責(zé)在主、從服務(wù)器傳輸各種修改動(dòng)作的媒介是主服務(wù)器的二進(jìn)制變更日志,這個(gè)日志記載著需要傳輸給從服務(wù)器的各種修改動(dòng)作。
因此,主服務(wù)器必須激活二進(jìn)制日志功能。從服務(wù)器必須具備足以讓它連接主服務(wù)器并請(qǐng)求主服務(wù)器把二進(jìn)制變更日志傳輸給它的權(quán)限。
配置主從復(fù)制的過程:
1).主節(jié)點(diǎn)操作步驟
(1)啟用二進(jìn)制日志
(2)設(shè)置一個(gè)在當(dāng)前集群中唯一的server-id;
(3)創(chuàng)建一個(gè)有復(fù)制權(quán)限(replication slave,replication client)帳號(hào);
2).slave節(jié)點(diǎn)的操作步驟
(1)啟用中繼日志;
(2)設(shè)置一個(gè)在當(dāng)前集群中唯一的server-id;
(3)使用有復(fù)制權(quán)限的用戶帳號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程;
注意:
(1)、服務(wù)器版本:主從服務(wù)器版本一致;
如果版本不一致,必須保證從服務(wù)器的版本高于主服務(wù)器的版本;
(2)、如果mariadb數(shù)據(jù)庫的隔離級(jí)別為可讀,其二進(jìn)制日志格式盡量使用基于行的;
實(shí)驗(yàn)環(huán)境:
服務(wù)器版本為:
CentOS 7.2 64bit
數(shù)據(jù)庫軟件版本為:
mariadb-5.5.52
db01節(jié)點(diǎn)的IP地址:172.16.22.8
db02節(jié)點(diǎn)的IP地址:172.16.22.6
三.yum 安裝Mariadb數(shù)據(jù)庫
刪除全部MySQL/MariaDB相關(guān)的rpm包
MySQL 已經(jīng)不再包含在 CentOS 7 的源中,而改用了 MariaDB;
1、使用rpm -qa | grep mariadb搜索 MariaDB 現(xiàn)有的包:
如果存在,使用rpm -e --nodeps mariadb-*全部刪除
2、使用rpm -qa | grep mysql搜索 mysql現(xiàn)有的包:
如果存在,使用yum remove mysql mysql-server mysql-libs compat-mysql全部刪除;
我比較推薦使用系統(tǒng)自帶的mariadb,這樣直接yum安裝即可
3、yum安裝mariadb
[root@~]# yum install -y mariadb mariadb-server
#注:mariadb為數(shù)據(jù)庫客戶端,mariadb-server是數(shù)據(jù)庫服務(wù)器端
4、命令啟動(dòng)MariaDB 服務(wù)
[root@~]# systemctl start mariadb
5、查看mariadb進(jìn)程服務(wù)是否在跑
[root@~]# ps -ef | grep mariadb
root 10131 10095 0 19:24 pts/2 00:00:00 grep --color=auto mariadb
mysql 10197 10009 0 Jan19 ? 15:56:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/lib/mysql/zgz.pid --socket=/var/lib/mysql/mysql.sock
6、查看數(shù)據(jù)庫狀態(tài)
[root@~]# systemctl status mariadb
7、接著運(yùn)行 mysql_secure_installation初始化配置MariaDB:
我覺得可以在這個(gè)階段修改設(shè)定數(shù)據(jù)庫密碼,除了Disallow root login remotely 、Remove test database and accesss to it 可以為n,其他都是y。主要看你個(gè)人的實(shí)際需求進(jìn)行設(shè)定。
8、登錄 MariaDB 并創(chuàng)建相應(yīng)的數(shù)據(jù)庫用戶與數(shù)據(jù)庫
(1)使用mysql -uroot -p登錄,回車之后會(huì)提示輸入密碼。
(2)創(chuàng)建新用戶,CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';其中$password填寫自己設(shè)置的密碼。當(dāng)然后面也可以修改;
(3)設(shè)置存儲(chǔ)引擎
mariadb[none]> set storage_engine=INNODB;
(4)創(chuàng)建數(shù)據(jù)庫
mariadb[none]>create database database_name character set utf8;
(5)設(shè)置用戶權(quán)限
grant all privileges on *.* to 'root'@'%' identified by '$password' with option;
#上述可以定義為使root用戶授予任何機(jī)器都可以遠(yuǎn)程連接的最高權(quán)限,并具有授予其他人連接權(quán)。
#注:主從數(shù)據(jù)庫Mariadb都可以按照上述安裝方式安裝即可。
四.數(shù)據(jù)庫主從復(fù)制
1.主節(jié)點(diǎn)db01配置
對(duì)master進(jìn)行配置,包括打開二進(jìn)制日志,指定唯一的servr ID。
vim /etc/my.cnf,在[mysqld]這個(gè)配置段加入如下內(nèi)容:
[mysqld]
#二進(jìn)制變更日志
log-bin=mysql-bin
#二進(jìn)制日志格式為混合模式
binlog_format=mixed
#為主服務(wù)器db01的ID值
server-id = 1
2.從節(jié)點(diǎn)db02配置:
配置/etc/my.cnf在[mysqld]這個(gè)配置段加入如下內(nèi)容:
log-bin=mysql-bin
binlog_format=mixed
server-id = 12
relay-log = relay-bin
log_slave_updates = 1
read_only = on
#注:
server_id是必須且唯一。slave沒有必要開啟二進(jìn)制日志,但是在一些情況下,必須設(shè)置;
如果slave為其它slave的master,必須設(shè)置bin_log。在這里,我們開啟了二進(jìn)制日志,而且顯示的命名(默認(rèn)名稱為hostname,但是,如果hostname改變則會(huì)出現(xiàn)問題)。
relay_log配置中繼日志,log_slave_updates表示slave將復(fù)制事件寫進(jìn)自己的二進(jìn)制日志(后面會(huì)看到它的用處)。
有些人開啟了slave的二進(jìn)制日志,卻沒有設(shè)置log_slave_updates,然后查看slave的數(shù)據(jù)是否改變,這是一種錯(cuò)誤的配置。故盡量使用read_only,它防止改變數(shù)據(jù)(除了特殊的線程)。但是,read_only并不是很實(shí)用,特別是那些需要在slave上創(chuàng)建表的應(yīng)用。
3.在Master節(jié)點(diǎn)db01的數(shù)據(jù)庫中建立一個(gè)備份帳戶:每個(gè)slave使用標(biāo)準(zhǔn)的MySQL用戶名和密碼連接master。進(jìn)行復(fù)制操作的用戶會(huì)授予REPLICATION SLAVE權(quán)限。用戶名的密碼都會(huì)存儲(chǔ)在文本文件master.info中
命令如下:
[root@db01 ~]# mysql -u root -p
#建立一個(gè)帳戶mysql_sync,并且只能允許172.16.22.6這個(gè)IP來登陸,密碼是mysql_sync。
MariaDB [(none)]> grant replication client,replication slave on *.* to 'mysql_sync'@'172.16.22.6' identified by 'mysql_sync';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4.拷貝數(shù)據(jù)
關(guān)停Master服務(wù)器,將Master中的數(shù)據(jù)拷貝到從服務(wù)器節(jié)點(diǎn)中,使得Master和slave中的數(shù)據(jù)同步,并且確保在全部設(shè)置操作結(jié)束前,禁止在Master和slave服務(wù)器中進(jìn)行寫操作,使得兩數(shù)據(jù)庫中的數(shù)據(jù)一定要相同!
但如果你的兩臺(tái)服務(wù)器是屬于完全新安裝的MariaDB主從服務(wù)器,這個(gè)一步就不需要。因?yàn)樾陆ǖ臄?shù)據(jù)庫沒有什么數(shù)據(jù)。
5.主從節(jié)點(diǎn)都重啟mysqld服務(wù);
主節(jié)點(diǎn):
[root@db01 ~]# systemctl restart mariadb
從節(jié)點(diǎn):
[root@db02 ~]# systemctl restart mariadb
6.啟動(dòng)從節(jié)點(diǎn)的中繼日志
接下來就是讓slave連接master,并開始重做master二進(jìn)制日志中的事件。使用CHANGE MASTER TO語句取代對(duì)配置文件的修改,而且它可以為slave指定不同的master,而不需要停止服務(wù)器。
命令介紹如下:使用help change master to即可查看命令選項(xiàng)
CHANGE MASTER TO option [, option] ...
option:
MASTER_BIND = 'interface_name'
| MASTER_HOST = 'host_name' 主服務(wù)器地址
| MASTER_USER = 'user_name' 有復(fù)制權(quán)限的用戶名
| MASTER_PASSWORD = 'password' 用戶密碼
| MASTER_PORT = port_num 主服務(wù)器的端口
| MASTER_CONNECT_RETRY = interval 連接重試時(shí)間間隔
| MASTER_HEARTBEAT_PERIOD = interval 心跳檢測(cè)時(shí)間間隔
| MASTER_LOG_FILE = 'master_log_name' 主服務(wù)器二進(jìn)制日志文件
| MASTER_LOG_POS = master_log_pos 二進(jìn)制日志文件中的位置
| RELAY_LOG_FILE = 'relay_log_name' 中繼日志文件名稱定義
| RELAY_LOG_POS = relay_log_pos 中繼日志時(shí)間點(diǎn)定義
| MASTER_SSL = {0|1} 下面都都是跟SSL安全傳輸相關(guān)的
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
| MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
| IGNORE_SERVER_IDS = (server_id_list)
需要記錄的二進(jìn)制日志文件的時(shí)間點(diǎn)是最后的那個(gè)點(diǎn),我們先去主服務(wù)器節(jié)點(diǎn)查看哪個(gè)時(shí)間點(diǎn),因?yàn)槭切路?wù)器,主節(jié)點(diǎn)上沒什么重要數(shù)據(jù):
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 426 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
啟動(dòng)從節(jié)點(diǎn)的中繼日志:
[root@db02 ~]# mysql -uroot -p$password
#查看只讀打開與否;
MariaDB [(none)]> show global variables like '%read_only%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
3 rows in set (0.01 sec)
#啟動(dòng)中繼日志的命令如下;
MariaDB [(none)]> change master to master_host='172.16.22.8',master_user='mysql_sync',master_password='mysql_sync',master_log_file='mysql-bin.000007',master_log_pos=426,master_connect_retry=5,master_heartbeat_period=2;
Query OK, 0 rows affected (0.07 sec)
#查看從服務(wù)器狀態(tài);
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.22.8
Master_User: mysql_sync
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 426
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: No
Slave_SQL_Running: No
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: 426
Relay_Log_Space: 248
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: NULL
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: 0
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
1 row in set (0.00 sec)
#通過查看從服務(wù)器狀態(tài),我們知道了從服務(wù)器的IO線程和SQL線程還未開啟,下面我們就開啟從服務(wù)器節(jié)點(diǎn)的復(fù)制進(jìn)程,實(shí)現(xiàn)主從復(fù)制;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
#再次查看從服務(wù)器狀態(tài),主要關(guān)注IO線程和SQL線程的開啟狀況:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.22.8
Master_User: repluser
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 426
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 535
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes #IOthread是否運(yùn)行,如果為No代表slave運(yùn)行不正常
Slave_SQL_Running: Yes #SQLthread是否運(yùn)行,如果為No代表slave運(yùn)行不正常
……中間信息略......
Seconds_Behind_Master: 0
slave的I/O和SQL線程都已經(jīng)開始運(yùn)行,而且Seconds_Behind_Master不再是NULL。日志的位置增加了,意味著一些事件被獲取并執(zhí)行了。如果你在master上進(jìn)行修改,你可以在slave上看到各種日志文件的位置的變化,同樣,你也可以看到數(shù)據(jù)庫中數(shù)據(jù)的變化。測(cè)試在下面進(jìn)行,我們先來看看主從節(jié)點(diǎn)的線程的狀態(tài);
7.查看master和slave上線程的狀態(tài)。在master上,你可以看到slave的I/O線程創(chuàng)建的連接:
在Master節(jié)點(diǎn)db01上輸入show processlist\G;
MariaDB [(none)]> show processlist \G
*************************** 1. row ***************************
Id: 5
User: mysql_sync
Host: 172.16.22.6:52875
db: NULL
Command: Binlog Dump
Time: 477
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Progress: 0.000
*************************** 2. row ***************************
Id: 6
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
Progress: 0.000
2 rows in set (0.00 sec)
1.row為處理slave的I/O線程的連接。
在Slave服務(wù)器節(jié)點(diǎn)db02上運(yùn)行該語句:
MariaDB [(none)]> show processlist \G
*************************** 1. row ***************************
Id: 5
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
Progress: 0.000
*************************** 2. row ***************************
Id: 6
User: system user
Host:
db: NULL
Command: Connect
Time: 587
State: Waiting for master to send event
Info: NULL
Progress: 0.000
*************************** 3. row ***************************
Id: 7
User: system user
Host:
db: NULL
Command: Connect
Time: 587
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
Progress: 0.000
3 rows in set (0.00 sec)
上述的2.row為SQL線程狀態(tài)、3.row為I/O線程狀態(tài)。
8.主從復(fù)制測(cè)試;
我們?cè)谥鞴?jié)點(diǎn)db01上創(chuàng)建數(shù)據(jù)庫表格進(jìn)入到test數(shù)據(jù)庫
數(shù)據(jù)庫存儲(chǔ)引擎盡量是innodb
創(chuàng)建表格完成后我們到主節(jié)點(diǎn)查看:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.03 sec)
MariaDB [(none)]> use test
Database changed
MariaDB [test]> show tables;
+-------------------+
| Tables_in_test |
+-------------------+
| test |
+-------------------+
1 rows in set (0.00 sec)
到從節(jié)點(diǎn)查看復(fù)制狀態(tài),可以看出復(fù)制已經(jīng)進(jìn)行了;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.22.8
Master_User: mysql_sync
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 8640
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 8849
Relay_Master_Log_File: mysql-bin.000007
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: 8640
Relay_Log_Space: 9140
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_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
1 row in set (0.00 sec)
查看從節(jié)點(diǎn)的數(shù)據(jù)庫復(fù)制完成與否,可以查看出我們導(dǎo)入的test數(shù)據(jù)庫表格是存在的:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table test;
MariaDB [test]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| test |
+-------------------+
1 rows in set (0.01 sec)
發(fā)現(xiàn)測(cè)試復(fù)制是成功的;
至此,數(shù)據(jù)庫的主從復(fù)制就完成了.
網(wǎng)頁標(biāo)題:Mariadb5.5.52主從復(fù)制集群的安裝步驟
網(wǎng)頁鏈接:http://muchs.cn/article48/iioohp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、App開發(fā)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)