(一)、背景介紹
因爲數據庫採用阿里雲的RDS數據庫,當全部的讀寫都在一臺服務器上的時候,出現了CPU負載較高,尤爲在業務時間段內,併發的訪問較多,形成業務壓力比較大,經詢問能夠採用主從複製,RDS數據庫只用於寫,其餘的本地安裝的mysql數據庫用於讀,能夠有多臺,後臺的程序還能夠作讀寫分離,來減輕RDS數據庫的壓力負載。mysql
安裝條件:linux
(二)、安裝配置
一、mysql數據庫的安裝和配置(略),具體參考https://blog.51cto.com/liqingbiao/1692315
二、替換/etc/my.cnf配置內容,其中跳過權限表進行啓動nginx
[appuser@inside-nginx152245 ~]$ cat /etc/my.cnf [mysqld] innodb_checksum_algorithm=crc32 innodb_data_file_path=ibdata1:200M:autoextend innodb_log_files_in_group=2 innodb_log_file_size=524288000 innodb_undo_directory=/usr/local/mysql/ basedir=/usr/local/mysql datadir=/data/mysql innodb_undo_tablespaces=0 server_id=999999998 log_bin=mysql-log gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 relay_log=relay-log sql_mode='' binlog_format=row slave_skip_errors = 1062,1032 skip-grant-tables=1 #######注意server_id能夠本身改,務必不要和RDS的同樣了
二、安裝配置Percona
一、安裝Percona 倉庫
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
二、查看percona庫
[appuser@predb ~]$ yum list|grep percona
三、安裝percona-xtrabackup-24包
yum install percona-xtrabackup-24sql
(二)、rpm 安裝數據庫
2.一、下載percona rpm包 wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm 2.二、經過yum localinstall 進行安裝 yum localinstall percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm #####5.6版本安裝這個 yum -y install percona-xtrabackup-24.x86_64 #####5.7版本須要安裝這個 2.三、percona卸載 yum remove percona-xtrabackup
三、下載備份文件並恢復。登陸雲服務器ECS,執行下載命令。wget -c '<數據備份文件外網下載地址>' -O <自定義文件名>.tar.gz -c啓用斷點續傳模式,-O將下載結果保存指定文件。vim
[root@inside-nginx152245 data]#wget -c 'http://rxxxxxxxxxxxxxxxxxxxx/custins4543777/hins6010151_data_20190621021827.tar.gz?OSSAccessKeyId=LTAIyKzxtSYNknVO&Expires=1561261206&Signature=KgiZPWl9YlX5kOX0WChx3h6Yw18%3D' -O mysql20190621.tar.gz [root@inside-nginx152245 data]# tar xf mysql20190621.tar.gz -C mysql [root@inside-nginx152245 data]# innobackupex --defaults-file=/etc/my.cnf --apply-log /data/mysql20190402/ 190621 16:16:41 innobackupex: Starting the apply-log operation IMPORTANT: Please check that the apply-log run completes successfully. At the end of a successful apply-log run innobackupex prints "completed OK!". [root@inside-nginx152245 data]# chown -R mysql:mysql /data/mysql [root@inside-nginx152245 data]# /etc/init.d/mysqld restart Shutting down MySQL.... [ OK ] Starting MySQL......... [ OK ]
四、直接進入mysql更新root密碼centos
mysql> update user set password=PASSWORD('xxxxx') where user='root'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) ##############通常使用########## mysql>update user set host='localhost' where host='::1'; mysql>update user set host='%' where host='127.0.0.1';
五、刪除/etc/my.cnf裏面的skip-grant-tables=1,而後重啓服務器
[root@inside-nginx152245 data]# vim /etc/my.cnf [root@inside-nginx152245 data]# /etc/init.d/mysqld restart Shutting down MySQL.. [ OK ] Starting MySQL. [ OK ]
六、經過root進行登陸,執行以下操做。併發
mysql>use mysql; ## 5.6執行 mysql>drop table slave_master_info; ### 5.6執行 mysql>drop table slave_relay_log_info; ### 5.6執行 mysql>drop table slave_worker_info; ###5.6執行 mysql>drop table innodb_index_stats; ### 5.6執行 mysql>drop table innodb_table_stats; ###5.6執行 mysql>source /usr/local/mysql/share/mysql_system_tables.sql; ##5.6執行, mysql>exit /etc/init.d/mysqld restart ###########5.6版本必須重啓,切記執行
備註:若是yum安裝的話,能夠找安裝路徑下mysql_system_tables.sql這個文件通常都在share目錄下。mysql> source /tmp/mysql-5.6.43-linux-glibc2.12-x86_64/share/mysql_system_tables.sql #######5.6版本執行。後邊的路徑是mysql安裝的路徑
七、查看xtrbackup_slave_info裏邊的set global gtid_purged語句,把這個copy到後面複製用app
[root@inside-nginx152245 share]# cat /data/mysql/xtrabackup_slave_info SET GLOBAL gtid_purged='5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344, 70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4846380, 7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134'; CHANGE MASTER TO MASTER_AUTO_POSITION=1;
三、在數據庫中依次執行以下語句
mysql> reset master -> ; Query OK, 0 rows affected (0.02 sec) mysql> SET GLOBAL gtid_purged='5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344, 70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4846380, 7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134'; Query OK, 0 rows affected (0.02 sec) mysql> reset slave; ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> reset slave; Query OK, 0 rows affected (0.02 sec) mysql> change master to master_host ='rm-uf64n5y6yet9b22s1.mysql.rds.aliyuncs.com',master_port=3306,master_user='dowadmin',master_password="70537449c232!dow",master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.04 sec) #############注意:上面的master_host master_port master_user master_password 根據RDS主庫的信息各自填寫本身的信息,不要照着上面執行; mysql> start slave; Query OK, 0 rows affected (0.02 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: rm-uf64n5y6yet9b22s1.mysql.rds.aliyuncs.com Master_User: dowadmin Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.001807 Read_Master_Log_Pos: 906900 Relay_Log_File: relay-log.000004 Relay_Log_Pos: 431601 Relay_Master_Log_File: mysql-bin.001805 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: 431471 Relay_Log_Space: 20108553 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: 35647 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: 459412040 Master_UUID: 70f8ce71-31fb-11e8-a373-7cd30ae014ac Master_Info_File: /data/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Reading event from the relay log Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 70f8ce71-31fb-11e8-a373-7cd30ae014ac:4846381-4867752 Executed_Gtid_Set: 5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344, 70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4848839, 7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134 Auto_Position: 1 1 row in set (0.00 sec) ERROR: No query specified #####搭建完成show slave status\G看到的結果應該以下,slave io/sql線程都是YES狀態:
若有問題,請查看阿里雲rds數據庫文檔(https://yq.aliyun.com/articles/689032?spm=a2c4e.11153940.0.0.48991670fK7Lg2)