MySQL主從複製(Master-Slave)與讀寫分離(MySQL-Proxy)實踐mysql
Mysql做爲目前世界上使用最普遍的免費數據庫,相信全部從事系統運維的工程師都必定接觸過。但在實際的生產環境中,由單臺Mysql做爲獨立的數據庫是徹底不能知足實際需求的,不管是在安全性,高可用性以及高併發等各個方面。sql
所以,通常來講都是經過 主從複製(Master-Slave)的方式來同步數據,再經過讀寫分離(MySQL-Proxy)來提高數據庫的併發負載能力 這樣的方案來進行部署與實施的。數據庫
以下圖所示:
vim
操做步鄹:安全
準備2臺機子 分別啓動數據庫,一臺爲master,一臺slavebash
master 10.0.0.1 3000服務器
slave 10.0.0.2 3003併發
一.修改master配置文件 vim /etc/my.confapp
log-bin = 3306-bin 開啓bin-log,也能夠指定路徑運維
erver-id = 1 惟一標示
二.在master庫建立個用戶容許從庫來同步
mysql> grant replication slave on *.* to 'rep'@'60.206.137.244' identified by 'test123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement ####若是報錯mysql> flush privileges;下在執行建立用戶
# mysqldump -h127.0.0.1 -uroot -poldboy -P3000 -A -B --master-data=2 --events > /tmp/mysql.sql ##把主的數據備份出來
# mysql -h127.0.0.1 -uroot -poldboy -P3003 < mysql.sql ##把主庫的數據導入到從庫
三,修改從配置文件
修改slave配置文件 vim /etc/my.conf
log-bin = 3306-bin 開啓bin-log,也能夠指定路徑
server-id = 2 惟一標示 ##主從要不同
經過命令行登陸管理MySQL服務器
mysql -uroot -p'密碼'
執行從庫SQL語句
change master to master_host='60.206.137.244', master_port=3000, master_user='rep', master_password='test123', master_log_file='3306-bin.000001', master_log_pos=333;
正確執行後啓動Slave同步進程
mysql> start slave;
主從同步檢查
mysql> show slave status\G
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 60.206.137.244 Master_User: rep Master_Port: 3000 Connect_Retry: 60 Master_Log_File: 3306-bin.000001 Read_Master_Log_Pos: 606 Relay_Log_File: 3308-relay-bin.000002 Relay_Log_Pos: 555 Relay_Master_Log_File: 3306-bin.000001 Slave_IO_Running: Yes ###若是是OK 證實成功 Slave_SQL_Running: Yes ###若是是OK 證實成功 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: 606 Relay_Log_Space: 727 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相差的數據差 通常是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_UUID: 1f18ad9e-8e2d-11e8-9752-5254005c6bc7 Master_Info_File: /data/ops/app/mysql-5.6.23/3308/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
驗證:
mysql> create database test; ##主庫建立庫
Query OK, 1 row affected (0.00 sec)
mysql> show databases; ##從庫能收到數據證實成功