1、單個數據庫服務器的缺點mysql
2、如何解決單點問題linux
3、MySQL主從複製架構
一、主庫將變動寫入到主庫的binlog中
sql
二、從庫的IO線程在指定位置讀取主庫binlog內容存儲到本地的中繼日誌(Relay Log)中數據庫
可使用mysqlbinlog來讀取relay log中的內容。
三、從庫的SQL線程讀取Relay Log日誌中的內容,並在從庫中重放
SQL線程所執行的事件,咱們能夠經過配置選項來決定是否要寫入到從服務器的二進制日誌中。
目前MySQL支持兩種複製類型:vim
雖然主從複製增長了一個數據庫副本,但從數據庫和主數據庫的數據最終會是一致的。之因此說是最終一致,由於MySQL複製是異步的,正常狀況下主從複製數據之間會有一個微小的延遲。
經過這個數據庫副本看似解決了數據庫單點問題,但並不完美:由於這種架構下,若是主服務器宕機,須要手動切換從服務器,業務中斷不能忍受,不能知足應用高可用的要求。服務器
在現代企業中,數據顯得尤其重要,而存儲數據的數據庫選擇又五花八門,但不管是何種數據庫,均存在着一種隱患。
想幾個問題:架構
1.2 主從形式異步
主從複製步驟:socket
主從複製配置步驟:ide
需求:
搭建兩臺MySQL服務器,一臺做爲主服務器,一臺做爲從服務器,主服務器進行寫操做,從服務器進行讀操做
環境說明:Red Hat Enterprise Linux 8.0 (Ootpa)
分別在主從兩臺機上二進制安裝mysql-5.7版本,此處略過安裝步驟
爲確保從數據庫與主數據庫裏的數據同樣,先全備主數據庫並還原到從數據庫中
//查看數據庫 主庫的數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wlw | | www | +--------------------+ 6 rows in set (0.00 sec) 從庫的數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) //全備主庫 mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) //備份主庫並將備份文件傳送到從庫 [root@zhu ~]# mysqldump -uroot -pw --all-databases > /opt/all-202005071900.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@zhu ~]# ls /opt/ 1 all-202005071900.sql data [root@zhu ~]# scp /opt/all-202005071900.sql root@192.168.66.128:/opt The authenticity of host '192.168.66.128 (192.168.66.128)' can't be established. ECDSA key fingerprint is SHA256:lpKqeNs7QFkySWsYsJ1IMnidcmZotljVvyB/y1YuWW4. ECDSA key fingerprint is MD5:ad:d8:3d:e7:7e:aa:fd:07:79:3c:7f:ca:82:0c:42:2f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.66.128' (ECDSA) to the list of known hosts. root@192.168.66.128's password: all-202005071900.sql 100% 774KB 47.5MB/s 00:00 //在從庫上恢復主庫的備份並查看,與主庫保持一致 [root@cong ~]# ls /opt/ all-202005071900.sql data [root@cong ~]# mysql < /opt/all-202005071900.sql [root@cong ~]# mysql -e 'show databases;' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wlw | | www | +--------------------+
//在主庫裏面建立一個同步帳號受權給從數據庫 mysql> create user 'wlw'@'192.168.66.128' identified by 'w' ; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'wlw'@'192.168.66.128'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
//配置主庫 [root@zhu ~]# vim /etc/my.cnf [mysql] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve log-bin=mysql-bin //啓用binlog日誌 server-id=1 //數據庫服務器惟一的標識符,主庫的值必定要大於從庫 relay-log=mysql-relay-bin symbolic-links=0 log-error=/var/log/mysqld.log //重啓mysql服務 [root@zhu ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! Failed to restart mysqld.service: Unit not found. [root@zhu ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 80 :::3306 :::* [root@zhu ~]# systemctl stop firewalld [root@zhu ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@zhu ~]# vim /etc/selinux/config [root@zhu ~]# setenforce 0
//配置從庫 [root@cong ~]# vim /etc/my.cnf [mysql] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve log-bin=mysql-bin //啓用binlog日誌 server-id=2 //數據庫服務器惟一的標識符,主庫的值必定要大於從庫 relay-log=mysql-relay-bin symbolic-links=0 log-error=/var/log/mysqld.log //重啓mysql服務 [root@cong ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@cong ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* [root@cong ~]# systemctl stop firewalld [root@cong ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@cong ~]# vim /etc/selinux/config [root@cong ~]# setenforce 0 LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 80 :::3306 :::*
//配置並啓動主從複製 mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.66.128', -> MASTER_USER='wlw', -> MASTER_PASSWORD='w', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=154; Query OK, 0 rows affected, 2 warnings (0.33 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) //查看服務器狀態 mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.66128 Master_User: wlw Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes //ྌ॒ᶳԅYes Slave_SQL_Running: Yes //ྌ॒ᶳԅYes Replicate_Do_DB: Replicate_Ignore_DB:
//驗證 在主庫表中建立內容 mysql> use wlw; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from w; +----+------+------+ | id | name | age | +----+------+------+ | 1 | q | 20 | | 2 | w | 23 | | 3 | e | 25 | | 4 | r | 28 | +----+------+------+ 4 rows in set (0.00 sec) mysql> insert into w values (5,'sean',20),(6,'tom',23),(7,'jerry',30); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from w; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | q | 20 | | 2 | w | 23 | | 3 | e | 25 | | 4 | r | 28 | | 5 | sean | 20 | | 6 | tom | 23 | | 7 | jerry | 30 | +----+-------+------+ 7 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) 在從庫查看是否同步 mysql> select * from w; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | q | 20 | | 2 | w | 23 | | 3 | e | 25 | | 4 | r | 28 | | 5 | sean | 20 | | 6 | tom | 23 | | 7 | jerry | 30 | +----+-------+------+ 7 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)