mysql的主從複製,是用來創建一個和主數據庫徹底同樣的數據庫環境,從庫會同步主庫的全部數據,可輕鬆實現故障轉移。node
MySQL主從複製是一個異步的複製過程,主庫發送更新事件到從庫,從庫讀取更新記錄,並執行更新記錄,使得從庫的內容與主庫保持一致。mysql
binlog:binary log,主庫中保存全部更新事件日誌的二進制文件。binary log是從數據庫服務啓動的一刻起,保存數據庫全部變動記錄(數據庫結構和內容)的文件。在主庫中,只要有更新事件出現,就會被依次地寫入到binary log中,以後會推送到從庫中做爲從庫進行復制的數據源。sql
binlog輸出線程:每當有從庫鏈接到主庫的時候,主庫都會建立一個線程而後發送binlog內容到從庫。 對於每個即將發送給從庫的sql事件,binlog輸出線程會將其鎖住。一旦該事件被線程讀取完以後,該鎖會被釋放,即便在該事件徹底發送到從庫的時候,該鎖也會被釋放。數據庫
在從庫中,當複製開始時,從庫就會建立從庫I/O線程和從庫的SQL線程進行復制處理。安全
從庫I/O線程:當start slave語句在從庫開始執行以後,從庫建立一個I/O線程,該線程鏈接到主庫並請求主庫發送binlog裏面的更新記錄到從庫上。 從庫I/O線程讀取主庫的binlog輸出線程發送的更新並拷貝這些更新到本地文件,其中包括relay log文件。網絡
從庫的SQL線程:從庫建立一個SQL線程,這個線程讀取從庫I/O線程寫到relay log的更新事件並執行。架構
綜上所述,可知:併發
對於每個主從複製的鏈接,都有三個線程。擁有多個從庫的主庫爲每個鏈接到主庫的從庫建立一個binlog輸出線程,每個從庫都有它本身的I/O線程和SQL線程。異步
從庫經過建立兩個獨立的線程,使得在進行復制時,從庫的讀和寫進行了分離。所以,即便負責執行的線程運行較慢,負責讀取更新語句的線程並不會所以變得緩慢。好比說,若是從庫有一段時間沒運行了,當它在此啓動的時候,儘管它的SQL線程執行比較慢,它的I/O線程能夠快速地從主庫裏讀取全部的binlog內容。這樣一來,即便從庫在SQL線程執行完全部讀取到的語句前中止運行了,I/O線程也至少徹底讀取了全部的內容,並將其安全地備份在從庫本地的relay log,隨時準備在從庫下一次啓動的時候執行語句。socket
node3:master,192.168.48.183 node4:slave, 192.168.48.184
xxxxxxxxxx # 安裝好mysql/mariadb數據庫: [root@node03 ~]# yum install mariadb mariadb-server -y # 修改/etc/my.cnf配置文件,在[mysqld]指令段添加如下行: log-bin=node3-bin server-id=1 # 啓動數據庫服務: [root@node03 ~]# systemctl start mariadb [root@node03 ~]# # 查看mysql進程: [root@node03 ~]# ps -ef|grep mysqld mysql 6130 1 0 20:37 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr mysql 6316 6130 0 20:37 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock root 6365 5819 0 20:38 pts/0 00:00:00 grep --color=auto mysqld [root@node03 ~]# # 查看mysql端口: [root@node03 ~]# netstat -ntlp|grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6316/mysqld [root@node03 ~]#
xxxxxxxxxx # 經過mysql直接進入數據庫: [root@node03 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> # 查看log_bin和sql_log_bin是否均爲on; MariaDB [(none)]> show variables like "%log_bin"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | | sql_log_bin | ON | +---------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]>
xxxxxxxxxx MariaDB [(none)]> grant replication slave on *.* to "superman"@"192.168.48.184" identified by "123456"; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]>
xxxxxxxxxx MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | node3-bin.000004 | 479 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) MariaDB [(none)]>
xxxxxxxxxx # 修改/etc/my.cnf配置文件,在[mysqld]指令塊下添加以下行: server-id=2
xxxxxxxxxx [root@node04 ~]# systemctl start mariadb
xxxxxxxxxx [root@node04 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> change master to -> master_host="192.168.48.183", -> master_user="superman", -> master_password="123456", -> master_log_file="node3-bin.000004", -> master_log_pos=479; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]>
xxxxxxxxxx MariaDB [(none)]> slave start; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.48.183 Master_User: superman Master_Port: 3306 Connect_Retry: 60 Master_Log_File: node3-bin.000004 Read_Master_Log_Pos: 479 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 529 Relay_Master_Log_File: node3-bin.000004 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: 479 Relay_Log_Space: 825 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) MariaDB [(none)]>
xxxxxxxxxx # 在主庫建立一個數據庫: MariaDB [(none)]> create database zabbix charset=utf8; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | zabbix | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> # 在從庫查看: MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | zabbix | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]>
xxxxxxxxxx Slave_IO_Running: Connecting # 第一種:主庫宕機 # 第二種:從庫指定的用戶名與密碼錯誤(與主庫受權的用戶名和密碼不一致) # 第三種:關閉防火牆 Slave_IO_Running: No # 從庫指定的二進制文件有誤 Slave_SQL_Running: No # pos點問題
建議從庫數量3-5 爲宜,要複製的從節點數量過多,會致使複製延遲。
從庫硬件比主庫差,致使複製延遲,查看master和slave的系統配置,可能會由於機器配置的問題,包括磁盤IO、CPU、內存等各方面因素形成複製的延遲,通常發生在高併發大數據量寫入場景。
主從庫之間的網絡延遲,主庫的網卡、網線、鏈接的交換機等網絡設備均可能成爲複製的瓶頸,致使複製延遲。
xxxxxxxxxx # 備份zabbix數據庫中的全部表,可是不會自動生成建立zabbix數據庫的語句: [root@node03 ~]# mysqldump -uroot -p*** zabbix > zabbix_tables.sql [root@node03 ~]#
xxxxxxxxxx 備份zabbix數據庫中的全部表,而且會生成建立zabbix數據庫的SQL語句,也就是導入時不須要先建立數據庫: [root@node03 ~]# mysqldump -uroot -p*** --databases zabbix > zabbix_database.sql [root@node03 ~]#
xxxxxxxxxx [root@node03 ~]# mysqldump -uroot -p*** --databases zabbix mysql > zabbix_mysql_database.sql [root@node03 ~]#
xxxxxxxxxx [root@node03 ~]# mysqldump -uroot -p*** --all-databases > all_databases.sql [root@node03 ~]# 或者: [root@node03 ~]# mysqldump -uroot -p*** -A > all.sql [root@node03 ~]#
xxxxxxxxxx [root@node03 ~]# mysqldump -uroot -p*** --master-data zabbix > zabbix_pos.sql [root@node03 ~]#
xxxxxxxxxx [root@node03 ~]# mysqldump -uroot -p*** --master-data --flush-logs zabbix > zabbix_pos_flush.sql [root@node03 ~]#