您好,登錄后才能下訂單哦!
下文主要給大家帶來使用Altas軟件如何實現mysql主從復制讀寫分離,希望使用Altas軟件如何實現mysql主從復制讀寫分離能夠帶給大家實際用處,這也是我編輯這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
mysql讀寫分離原理:
數據庫層在高并發的情況下,i/o會產生瓶頸。而實際上用戶讀的請求要遠遠大于寫的請求。
使用代理服務作為數據庫前端,將不同的請求根據規則分配到不同的后端數據上面去,比如將寫的請求分配給master數據庫,將讀的請求分配給slave數據庫。master和slave可以是一個或多個做成負載均衡。master數據庫再通過主從復制同步數據給slave.
環境介紹:
HostName | OS | IP | 作用 |
master | centos6.5 | 192.168.100.150 | 擔任mysql主云服務器 |
salve | centos6.5 | 192.168.100.151 | 擔任mysql從云服務器 |
Altas | centos6.5 | 192.168.100.152 | 擔任mysql代理 |
ftp | centos6.5 | 192.168.100.100 | 擔任ftp為主從提供yum源,軟件支持(可以使用公網yum源代替此主機) |
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 ##服務的唯一標識符號,默認是1,這里方便記憶,我使用了ip最后一段 [root@slave ~]# vi /etc/my.cnf 7 server-id=151 [root@master ~]# /etc/init.d/mysqld start ##重啟服務 [root@slave ~]# /etc/init.d/mysqld start
3:在主數據庫上面授權給從復制的權限:
登陸云服務器授權
[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; ##刷新權限 Query OK, 0 rows affected (0.00 sec) mysql>
查看主服務的bin-log日志文件信息:
需要記錄file 和position兩欄中內容:以查到的為準。
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 476 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
4:在從云服務器上修改自己的master的數據庫
登入數據庫
[root@slave ~]# mysqladmin -uroot password 123123 [root@slave ~]# mysql -uroot -p123123
設置從云服務器讀取master bin-log的相關信息
mysql> change master to -> master_host='192.168.100.150', ##master的ip -> master_user='slave', ##授權允許復制的用戶名 -> master_password='123123', ##授權允許復制密碼 -> master_log_file='mysql-bin.000003', ##bin-log文件名,上一步在master上查到的信息 -> master_log_pos=476; ##偏移量,在master上查到的信息 Query OK, 0 rows affected (0.07 sec)
啟動slave
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
插卡slave狀態:
##查到的狀態這兩個為yes,下面沒有error錯誤就正常 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:測試:
在主數據庫上新建庫,查看庫
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)
在從數據庫上查看庫:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test_databases | +--------------------+ 4 rows in set (0.00 sec)
在master端授權:
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云服務器下載安裝軟件
[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 加上數據庫授權的密碼,生成密文密碼填寫在這里
生成密文密碼
[root@Atlas bin]# pwd /usr/local/mysql-proxy/bin [root@Atlas bin]# ./encrypt 123123 ++gAN07C/Q0=
daemon = true
sql-log = REALTIME
charset = utf8
全部的配置項,以及注釋
#管理接口的密碼 admin-password = pwd #Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端連接的MySQL從庫的IP和端口,@后面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = root:++gAN07C/Q0= #設置Atlas的運行方式,設為true時為守護進程方式,設為false時為前臺方式,一般開發調試時設為false,線上運行時設為true,true后面不能有空格。 daemon = true keepalive = true #工作線程數,對Atlas的性能有很大影響,可根據情況適當設置 event-threads = 8 #日志級別,分為message、warning、critical、error、debug五個級別 #帶#號的為非必需的配置項目 #管理接口的用戶名 admin-username = user #管理接口的密碼 admin-password = pwd #Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端連接的MySQL從庫的IP和端口,@后面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = root:++gAN07C/Q0= #設置Atlas的運行方式,設為true時為守護進程方式,設為false時為前臺方式,一般開發調試時設為false,線上運行時設為true,true后面不能有空格。 daemon = true keepalive = true #工作線程數,對Atlas的性能有很大影響,可根據情況適當設置 event-threads = 8 #日志級別,分為message、warning、critical、error、debug五個級別 log-level = message #日志存放的路徑 log-path = /usr/local/mysql-proxy/log #SQL日志的開關,可設置為OFF、ON、REALTIME,OFF代表不記錄SQL日志,ON代表記錄SQL日志,REALTIME代表記錄SQL日志且實時寫入磁盤,默認為OFF sql-log = REALTIME #慢日志輸出設置。當設置了該參數時,則日志只輸出執行時間超過sql-log-slow(單位:ms)的日志記錄。不設置該參數則輸出全部日志。 #sql-log-slow = 10 #實例名稱,用于同一臺機器上多個Atlas實例間的區分 #instance = test #Atlas監聽的工作接口IP和端口 proxy-address = 0.0.0.0:1234 #Atlas監聽的管理接口IP和端口 admin-address = 0.0.0.0:2345 #分表設置,此例中person為庫名,mt為表名,id為分表字段,3為子表數量,可設置多項,以逗號分隔,若不分表則不需要設置該項 #tables = person.mt.id.3 #默認字符集,設置該項后客戶端不再需要執行SET NAMES語句 charset = utf8 #允許連接Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號分隔,若不設置該項則允許所有IP連接,否則只允許列表中的IP連接 #client-ips = 127.0.0.1, 192.168.1 #Atlas前面掛接的LVS的物理網卡的IP(注意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置 #lvs-ips = 192.168.1.1
啟動關閉代理服務:
[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
查看進程:
[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數據庫了,master可以讀寫操作,一般是只寫。
登錄到管理端口:(可以對后端的mysql數據庫進行管理)
[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狀態,工作類型 +----------+----------------------+-------+------+-------------+ | 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)
對于以上關于使用Altas軟件如何實現mysql主從復制讀寫分離,大家是不是覺得非常有幫助。如果需要了解更多內容,請繼續關注我們的行業資訊,相信你會喜歡上這些內容的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。