您好,登錄后才能下訂單哦!
下文主要給大家?guī)硎褂肁ltas軟件如何實(shí)現(xiàn)mysql主從復(fù)制讀寫分離,希望使用Altas軟件如何實(shí)現(xiàn)mysql主從復(fù)制讀寫分離能夠帶給大家實(shí)際用處,這也是我編輯這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
mysql讀寫分離原理:
數(shù)據(jù)庫層在高并發(fā)的情況下,i/o會(huì)產(chǎn)生瓶頸。而實(shí)際上用戶讀的請(qǐng)求要遠(yuǎn)遠(yuǎn)大于寫的請(qǐng)求。
使用代理服務(wù)作為數(shù)據(jù)庫前端,將不同的請(qǐng)求根據(jù)規(guī)則分配到不同的后端數(shù)據(jù)上面去,比如將寫的請(qǐng)求分配給master數(shù)據(jù)庫,將讀的請(qǐng)求分配給slave數(shù)據(jù)庫。master和slave可以是一個(gè)或多個(gè)做成負(fù)載均衡。master數(shù)據(jù)庫再通過主從復(fù)制同步數(shù)據(jù)給slave.
環(huán)境介紹:
HostName | OS | IP | 作用 |
master | centos6.5 | 192.168.100.150 | 擔(dān)任mysql主云服務(wù)器 |
salve | centos6.5 | 192.168.100.151 | 擔(dān)任mysql從云服務(wù)器 |
Altas | centos6.5 | 192.168.100.152 | 擔(dān)任mysql代理 |
ftp | centos6.5 | 192.168.100.100 | 擔(dān)任ftp為主從提供yum源,軟件支持(可以使用公網(wǎng)yum源代替此主機(jī)) |
1:主從安裝mysql:
[root@master ~]# yum -y install mysql-server [root@slave ~]# yum -y install msyql-server
2:修改主從的配置文件,以支持bin_log日志記錄
[root@master ~]# vi /etc/my.cnf 7 log-bin=mysql-bin ##支持bin-log日志記錄,bin-log日志文件名以mysql-bin開頭 8 server-id=150 ##服務(wù)的唯一標(biāo)識(shí)符號(hào),默認(rèn)是1,這里方便記憶,我使用了ip最后一段 [root@slave ~]# vi /etc/my.cnf 7 server-id=151 [root@master ~]# /etc/init.d/mysqld start ##重啟服務(wù) [root@slave ~]# /etc/init.d/mysqld start
3:在主數(shù)據(jù)庫上面授權(quán)給從復(fù)制的權(quán)限:
登陸云服務(wù)器授權(quán)
[root@master ~]# mysqladmin -uroot password 123123 [root@master ~]# mysql -uroot -p123123 mysql> grant replication slave on *.* to 'slave'@"192.168.100.%" identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; ##刷新權(quán)限 Query OK, 0 rows affected (0.00 sec) mysql>
查看主服務(wù)的bin-log日志文件信息:
需要記錄file 和position兩欄中內(nèi)容:以查到的為準(zhǔn)。
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 476 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
4:在從云服務(wù)器上修改自己的master的數(shù)據(jù)庫
登入數(shù)據(jù)庫
[root@slave ~]# mysqladmin -uroot password 123123 [root@slave ~]# mysql -uroot -p123123
設(shè)置從云服務(wù)器讀取master bin-log的相關(guān)信息
mysql> change master to -> master_host='192.168.100.150', ##master的ip -> master_user='slave', ##授權(quán)允許復(fù)制的用戶名 -> master_password='123123', ##授權(quán)允許復(fù)制密碼 -> master_log_file='mysql-bin.000003', ##bin-log文件名,上一步在master上查到的信息 -> master_log_pos=476; ##偏移量,在master上查到的信息 Query OK, 0 rows affected (0.07 sec)
啟動(dòng)slave
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
插卡slave狀態(tài):
##查到的狀態(tài)這兩個(gè)為yes,下面沒有error錯(cuò)誤就正常 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.150 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 706 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 481 Relay_Master_Log_File: mysql-bin.000003 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: 706 Relay_Log_Space: 637 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: 1 row in set (0.00 sec) ERROR: No query specified mysql>
5:測(cè)試:
在主數(shù)據(jù)庫上新建庫,查看庫
mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> create database test_databases; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test_databases | +--------------------+ 4 rows in set (0.00 sec)
在從數(shù)據(jù)庫上查看庫:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test_databases | +--------------------+ 4 rows in set (0.00 sec)
在master端授權(quán):
mysql> grant all on *.* to root@"192.168.100.%" identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
在Atlas云服務(wù)器下載安裝軟件
[root@Atlas ~]# wget -O ./altas https://github.com/Qihoo360/Atlas/releases/download/sharding-1.0.1/Atlas-sharding_1.0.1-el6.x86_64.rpm
[root@Atlas ~]# ls altas anaconda-ks.cfg install.log install.log.syslog [root@Atlas ~]# file altas altas: RPM v3.0 bin i386/x86_64 Atlas-sharding_1.0.1-el6 [root@Atlas ~]# rpm -ivh altas Preparing... ########################################### [100%] 1:Atlas ########################################### [100%]
修改配置文件:
[root@Atlas ~]# vim /usr/local/mysql-proxy/conf/test.cnf
需要更改的地方:
proxy-backend-addresses = 192.168.100.150:3306
proxy-read-only-backend-addresses = 192.168.100.151:3306
pwds = root:++gAN07C/Q0= ##這里用/usr/local/mysql-proxy/bin/encrypt 加上數(shù)據(jù)庫授權(quán)的密碼,生成密文密碼填寫在這里
生成密文密碼
[root@Atlas bin]# pwd /usr/local/mysql-proxy/bin [root@Atlas bin]# ./encrypt 123123 ++gAN07C/Q0=
daemon = true
sql-log = REALTIME
charset = utf8
全部的配置項(xiàng),以及注釋
#管理接口的密碼 admin-password = pwd #Atlas后端連接的MySQL主庫的IP和端口,可設(shè)置多項(xiàng),用逗號(hào)分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端連接的MySQL從庫的IP和端口,@后面的數(shù)字代表權(quán)重,用來作負(fù)載均衡,若省略則默認(rèn)為1,可設(shè)置多項(xiàng),用逗號(hào)分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用戶名與其對(duì)應(yīng)的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = root:++gAN07C/Q0= #設(shè)置Atlas的運(yùn)行方式,設(shè)為true時(shí)為守護(hù)進(jìn)程方式,設(shè)為false時(shí)為前臺(tái)方式,一般開發(fā)調(diào)試時(shí)設(shè)為false,線上運(yùn)行時(shí)設(shè)為true,true后面不能有空格。 daemon = true keepalive = true #工作線程數(shù),對(duì)Atlas的性能有很大影響,可根據(jù)情況適當(dāng)設(shè)置 event-threads = 8 #日志級(jí)別,分為message、warning、critical、error、debug五個(gè)級(jí)別 #帶#號(hào)的為非必需的配置項(xiàng)目 #管理接口的用戶名 admin-username = user #管理接口的密碼 admin-password = pwd #Atlas后端連接的MySQL主庫的IP和端口,可設(shè)置多項(xiàng),用逗號(hào)分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端連接的MySQL從庫的IP和端口,@后面的數(shù)字代表權(quán)重,用來作負(fù)載均衡,若省略則默認(rèn)為1,可設(shè)置多項(xiàng),用逗號(hào)分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用戶名與其對(duì)應(yīng)的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = root:++gAN07C/Q0= #設(shè)置Atlas的運(yùn)行方式,設(shè)為true時(shí)為守護(hù)進(jìn)程方式,設(shè)為false時(shí)為前臺(tái)方式,一般開發(fā)調(diào)試時(shí)設(shè)為false,線上運(yùn)行時(shí)設(shè)為true,true后面不能有空格。 daemon = true keepalive = true #工作線程數(shù),對(duì)Atlas的性能有很大影響,可根據(jù)情況適當(dāng)設(shè)置 event-threads = 8 #日志級(jí)別,分為message、warning、critical、error、debug五個(gè)級(jí)別 log-level = message #日志存放的路徑 log-path = /usr/local/mysql-proxy/log #SQL日志的開關(guān),可設(shè)置為OFF、ON、REALTIME,OFF代表不記錄SQL日志,ON代表記錄SQL日志,REALTIME代表記錄SQL日志且實(shí)時(shí)寫入磁盤,默認(rèn)為OFF sql-log = REALTIME #慢日志輸出設(shè)置。當(dāng)設(shè)置了該參數(shù)時(shí),則日志只輸出執(zhí)行時(shí)間超過sql-log-slow(單位:ms)的日志記錄。不設(shè)置該參數(shù)則輸出全部日志。 #sql-log-slow = 10 #實(shí)例名稱,用于同一臺(tái)機(jī)器上多個(gè)Atlas實(shí)例間的區(qū)分 #instance = test #Atlas監(jiān)聽的工作接口IP和端口 proxy-address = 0.0.0.0:1234 #Atlas監(jiān)聽的管理接口IP和端口 admin-address = 0.0.0.0:2345 #分表設(shè)置,此例中person為庫名,mt為表名,id為分表字段,3為子表數(shù)量,可設(shè)置多項(xiàng),以逗號(hào)分隔,若不分表則不需要設(shè)置該項(xiàng) #tables = person.mt.id.3 #默認(rèn)字符集,設(shè)置該項(xiàng)后客戶端不再需要執(zhí)行SET NAMES語句 charset = utf8 #允許連接Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號(hào)分隔,若不設(shè)置該項(xiàng)則允許所有IP連接,否則只允許列表中的IP連接 #client-ips = 127.0.0.1, 192.168.1 #Atlas前面掛接的LVS的物理網(wǎng)卡的IP(注意不是虛IP),若有LVS且設(shè)置了client-ips則此項(xiàng)必須設(shè)置,否則可以不設(shè)置 #lvs-ips = 192.168.1.1
啟動(dòng)關(guān)閉代理服務(wù):
[root@Atlas bin]# ls encrypt mysql-binlog-dump mysql-myisam-dump mysql-proxy mysql-proxyd VERSION [root@Atlas bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started [root@Atlas bin]# ./mysql-proxyd test stop OK: MySQL-Proxy of test is stopped [root@Atlas bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started [root@Atlas bin]# ./mysql-proxyd test restart OK: MySQL-Proxy of test is stopped OK: MySQL-Proxy of test is started
查看進(jìn)程:
[root@Atlas ~]# ps aux |grep mysql-proxy root 1266 0.0 0.2 67156 1452 ? S 19:24 0:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf root 1267 0.0 0.6 161460 3352 ? Sl 19:24 0:01 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf root 16756 0.0 0.1 103248 876 pts/0 S+ 20:55 0:00 grep mysql-proxy
安裝mysql,只安裝客戶端。
[root@Atlas ~]# yum -y install mysql [root@Atlas ~]# mysql -uroot -p123123 -h 192.168.100.152 -P1234 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> quit Bye
查看日志信息:
[root@Atlas ~]# tail -f /usr/local/mysql-proxy/log/test.log
2017-08-22 19:24:32: (message) proxy listening on port 0.0.0.0:1234
2017-08-22 19:24:32: (message) added read/write backend: 192.168.100.150:3306
2017-08-22 19:24:32: (message) added read-only backend: 192.168.100.151:3306
2017-08-22 19:24:32: (message) chassis-event-thread.c:235: starting 8 threads
2017-08-22 19:24:34: (message) chassis-unix-daemon.c:138: [angel] we try to keep PID=1267 alive
2017-08-22 19:24:34: (message) mysql-proxy 0.8.2 started - instance: test
2017-08-22 19:24:34: (message) proxy listening on port 0.0.0.0:1234
2017-08-22 19:24:34: (message) added read/write backend: 192.168.100.150:3306
2017-08-22 19:24:34: (message) added read-only backend: 192.168.100.151:3306
2017-08-22 19:24:34: (message) chassis-event-thread.c:235: starting 8 threads
##可以看到讀的操作都交給slave數(shù)據(jù)庫了,master可以讀寫操作,一般是只寫。
登錄到管理端口:(可以對(duì)后端的mysql數(shù)據(jù)庫進(jìn)行管理)
[root@Atlas ~]# mysql -uuser -ppwd -h292.168.100.152 -P2345 mysql> select * from help; ##查看管理幫助 +---------------------------------------+---------------------------------------------------------+ | command | description | +---------------------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx's id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | ADD GMASTER $group_id $backend | example: "add gmaster 1 127.0.0.1:3306", ... | | ADD GSLAVE $group_id $backend | example: "add gslave 1 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | REMOVE GBACKEND $group_id $backend_id | example: "remove gbackend 1 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +---------------------------------------+---------------------------------------------------------+ 19 rows in set (0.00 sec) mysql> select * from backends; ##查看后端mysql狀態(tài),工作類型 +----------+----------------------+-------+------+-------------+ | group_id | address | state | type | backend_ndx | +----------+----------------------+-------+------+-------------+ | -1 | 192.168.100.150:3306 | up | rw | 1 | | -1 | 192.168.100.151:3306 | up | ro | 2 | +----------+----------------------+-------+------+-------------+ 2 rows in set (0.00 sec)
對(duì)于以上關(guān)于使用Altas軟件如何實(shí)現(xiàn)mysql主從復(fù)制讀寫分離,大家是不是覺得非常有幫助。如果需要了解更多內(nèi)容,請(qǐng)繼續(xù)關(guān)注我們的行業(yè)資訊,相信你會(huì)喜歡上這些內(nèi)容的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。