MYSQL主從複製

 

MYSQL主從複製原理

MySQL主從同步一共須要三個線程的操做,主MySQL有一個IO線程,從MySQL有一個IO線程和一個SQL線程, MySQL主從是實現MySQL高可用、數據備份、讀寫分離架構的一種最多見的解決方案,在絕大部分公司都有使用,要實現MySQL主從複製,必需要在Master打開binary log(bin-log)功能,由於整個MySQL的複製過程實際就是Slave從Master端獲取響應的二進制日誌,而後在Slave端順序的執行日誌中所記錄的各類操做,二進制日誌中幾乎記錄了出select之外的全部針對數據庫的sql操做語句,具體的複製過程以下:mysql

1.1.1:Slave端的IO線程鏈接上Master,並向Master請求指定日誌文件的指定位置(新部署的Master和Slave從最開始的日誌)以後的日誌。linux

1.1.2:Master接收到來自Slave的IO線程請求,負責IO複製的IO線程根據Slave的請求信息讀取相應的日誌內容,而後將本地讀取的bin-log的文件名、位置及指定位置以後的內容一塊兒返回給Slave的IO線程處理。c++

1.1.3:Slave的IO線程將接收到的信息依次添加到Slave端的relay-log文件的最末端,並將讀取到的Master端的bin-log的文件名和位置記錄到Master-info文件中,以便在下一次讀取的時候可以清楚的告訴Master「我須要從哪一個bin-log的哪一個位置開始日後的日誌內容請發給我」。sql

1.1.4:Slave的sql線程檢查到relay-log中新增了內容後,會立刻將relay-log中的內容解析爲在Master端真實執行時候的可執行命令,並順序執行,從而保證對Slave的MySQL進行響應的增長或刪除等操做,最終實現和Master數據保持一致。數據庫

配置MYSQL主從同步

Master:192.168.10.201 vim

Slave:192.168.10.202bash

[root@linux-host1 src]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 服務器

一、兩臺服務器分別安裝MySQL,提早準備好安裝包和my.conf文件,兩臺機器上使用如下腳本自動安裝:架構

[root@linux-host1 src]# vim mysql-install.sh 

#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"

yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion -y
yum install curl policycoreutils openssh-server openssh-clients postfix -y

if [ -f ${FULL_NAME} ];then
    echo "安裝文件存在"
else
    echo "安裝文件不存在"
    exit 3
fi
if [ -h /usr/local/mysql ];then
    echo "Mysql 已經安裝"
    exit 3
else
    tar xvf ${FULL_NAME}   -C /usr/local/src
    ln -sv /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64  /usr/local/mysql
    if id  mysql;then
        echo "mysql 用戶已經存在,跳過建立用戶過程"
    fi
        useradd  mysql  -s /sbin/nologin
    if  id  mysql;then
        chown  -R mysql.mysql  /usr/local/mysql/* -R
        if [ ! -d  /data/mysql ];then
            mkdir -pv /var/lib/mysql && chwn mysql.mysql /var/lib/mysql -R
            mkdir -pv /data/mysql/{data,logs} && touch  /data/mysql/logs/error.log && chown  -R mysql.mysql  /data   -R
            /usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql/data  --basedir=/usr/local/mysql/
            cp  /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/support-files/mysql.server /etc/init.d/mysqld
            chmod a+x /etc/init.d/mysqld
            cp ${DIR}/my.cnf   /etc/my.cnf
            ln -sv /usr/local/mysql/bin/mysql  /usr/bin/mysql
            /etc/init.d/mysqld start
        else
            echo "MySQL數據目錄已經存在,"
                        exit 3
        fi
    fi
fi

           

二、分別更改Master和Slave的my.conf文件,主要將server-id改成不同,Slave能夠不用開log-bin=mysql-bin註釋掉便可,修改完my.conf文件重啓兩臺機器的mysql服務:ssh

[root@linux-host1 src]# vim /usr/local/src/my.cnf 
[client]
port            = 3306
socket          = /data/mysql/data/mysql.sock
#default-character-set=utf8
[mysqld]
#skip-grant-tables
sync_binlog=0
innodb_flush_log_at_trx_commit=0
default-time-zone = '+8:00'
local-infile=0
skip-name-resolve
skip-external-locking
back_log = 300
max_connections = 1000 
max_allowed_packet = 32M
binlog_cache_size = 1M
max_heap_table_size = 64M
sort_buffer_size = 2M
query_cache_type=0
join_buffer_size = 2M
thread_cache = 8
ft_min_word_len = 4
thread_stack = 192K
tmp_table_size = 64M
port            = 3306
socket          = /data/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
table_open_cache = 400 
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir=/data/mysql/data
log-bin=mysql-bin
replicate-ignore-db=test
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog_format=row
server-id       =202
relay-log=relay-bin
relay-log-index=relay-bin
log-queries-not-using-indexes
long_query_time =1
slow_query_log=on
log_queries_not_using_indexes=off
slow_query_log_file=/data/mysql/logs/slow_query.log
log-error=/data/mysql/logs/error.log
key_buffer_size = 32M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 10M
myisam_repair_threads = 1
myisam_recover
innodb_buffer_pool_size=5000M
innodb_additional_mem_pool_size=32M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_log_buffer_size = 8M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table=1
innodb_open_files=500
character-set-server = utf8
wait_timeout=300
interactive_timeout=300
innodb_flush_method=O_DIRECT
log-bin-trust-function-creators=1
log_slave_updates = 1
relay-log = /data/mysql/logs/relay-log.log


[mysqldump]
quick
max_allowed_packet = 16M
user=root
password=root

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
#user=root
#password=root

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[root@linux-host1 src]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!

三、兩臺數據庫初始化,而後Master建立同步帳號syncuser,密碼123456

[root@linux-host2 src]# /usr/local/mysql/bin/mysql_secure_installation #兩臺數據庫執行這個命令初始化,設置root密碼123456

而後Master建立同步帳號syncuser,密碼123456

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'syncuser'@'192.168.10.%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

四、Master導出數據、scp到Slave:

[root@linux-host1 src]# /usr/local/mysql/bin/mysqldump -uroot -p123456 --all-databases --single_transaction --flush-logs --master-data=2 --lock-tables > /opt/backup.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-host1 src]# cd /opt/
[root@linux-host1 opt]# ll
total 648
-rw-r--r-- 1 root root 655442 Mar 28 20:52 backup.sql
-rw-r--r-- 1 root root 583 Mar 12 16:27 dele.py

[root@linux-host1 opt]# scp backup.sql root@192.168.10.202:/opt/
root@192.168.10.202's password:
backup.sql 100% 640KB 33.1MB/s 00:00

注意:Slave會用到Master如下兩個地方:

五、Slave將Master的sql文件導入並記錄Master的日誌文件名稱和要同步的位置:

[root@linux-host2 src]# mysql -uroot -p123456 < /opt/backup.sql
Warning: Using a password on the command line interface can be insecure.

六、在Slave配置從Master同步並設置Slave開啓 只讀:

mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.201',MASTER_USER='syncuser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

七、驗證Slave的IO線程與SQL線程是OK的:

八、在Slave驗證否同步完成,在Master數據庫建立一個SB的庫,而後在Slave數據庫查看也有一個SB的庫同步成功:

Slave建立SB這個庫:

Slave數據庫上查看也有SB這個庫,驗證MySQL主從同步成功;

相關文章
相關標籤/搜索