1.主從複製配置
a. 環境:CentOS7.4,IP地址分別是主庫:192.168.11.146,從庫:192.168.11.238,
主庫版本應低於或等於從庫版本,這裏用的都是MySQL 8.0.13
b.主庫配置/etc/my.cnf文件mysql
[mysqld]sql
#通常配置選項
user=mysql
port=3306
server_id=1
basedir= /usr/local/mysql
datadir= /usr/app/mysqldata服務器
character_set_server=utf8app
pid_file=/usr/app/mysqldata/mysql.pid
socket = /var/run/mysqld/mysqld.sock
#錯誤日誌
log_error= /usr/app/mysqldata/myerror.log
#慢日誌
slow_query_log=1
slow_query_log_file=/usr/app/mysqllog/mysql.slow
long_query_time=5
#bin_log日誌
log_bin=/usr/app/mysqllog/mysql_bin
skip_external_locking
skip_name_resolve
#開啓獨立表空間
innodb_file_per_table = onsocket
[mysql]
no-auto-rehash
socket = /var/run/mysqld/mysqld.sock
#default-character-set=utf8
#safe-updateside
[client]
loose-default-character-set = utf8線程
重啓MySQL服務rest
/etc/init.d/mysqld start日誌
受權slvave 用戶server
grant replication slave,replication client on . to 'slvae_rp' identified by '111111';
flush privileges;
從庫配置/etc/my.cnf文件
[mysqld]
#通常配置選項
user=mysql
port=3306
server_id=1
basedir= /usr/local/mysql
datadir= /usr/app/mysqldata
character_set_server=utf8
pid_file=/usr/app/mysqldata/mysql.pid
socket = /var/run/mysqld/mysqld.sock
#錯誤日誌
log_error= /usr/app/mysqldata/myerror.log
#慢日誌
slow_query_log=1
slow_query_log_file=/usr/app/mysqllog/mysql.slow
long_query_time=5
#bin_log日誌
log_bin=/usr/app/mysqllog/mysql_bin
#開啓中繼日誌
relay_log=/var/lib/mysql/relay-log
log_slave_updates=1 #從服務器將其SQL線程執行的更新記入到從服務器本身的二進制日誌
read_only=1 #只讀模式
skip_external_locking
skip_name_resolve
#開啓獨立表空間
innodb_file_per_table = on
[mysql]
no-auto-rehash
socket = /var/run/mysqld/mysqld.sock
#default-character-set=utf8
#safe-updates
[client]
loose-default-character-set = utf8
重啓mysql服務
/etc/init.d/mysqld restart
主庫:
show master status;
從庫:
change master to
master_host='192.168.11.146',
master_port=3306,
master_user='slave_rp',
master_password='111111',
master_log_file='mysql-bin.000001',
master_log_pos=154;
開啓從庫slava
start slave;
查看從庫狀態
show slave status;
確認正常:
slave_io_running:yes
slave_sql_running:yes
seconds_behind_master 0
2.主從複製原理
從庫生成兩個線程,一個I/O線程,一個SQL線程;
i/o線程去請求主庫 的binlog,並將獲得的binlog日誌寫到relay log(中繼日誌) 文件中;
主庫會生成一個 log dump 線程,用來給從庫 i/o線程傳binlog;
SQL 線程,會讀取relay log文件中的日誌,並解析成具體操做,來實現主從的操做一致,而最終數據一致;