Linux(CentOS7)配置 MySQL5.7 主從備份

配置主節點

①準備好兩臺已安裝MySQL的服務器

修改 Master 配置文件 /etc/my.cnf,在修改前建議複製一份備份文件

注意:

master中 server-id 爲1 是MySQL 服務唯一標識
唯一標識是數字. 自然數配置的時候有要求:

單機使用 server-id 任意配置,只要是數字即可
主從使用 server-id Master 唯一標識數字必須小於 Slave 唯一標識數字.

本環境中 log_bin 值 : master_log 日誌文件命名, 開啓日誌功能。此日誌是命令日誌。就是記錄主庫中執行的所有的 SQL命令的。
MySQL 的 log_bin 不是執行日誌,狀態日誌. 是操作日誌.就是在 DBMS 中所有的 SQL 命令,log_bin 日誌不
是必要的.只有配置主從備份時才必要。

修改後的 my.cnf 配置文件內容如下:

[client]
port = 3306
default-character-set = utf8mb4
 
[mysqld]
port = 3306

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid


user = mysql
bind-address = 0.0.0.0
server-id = 1
 
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
 
skip-name-resolve
skip-external-locking
#skip-networking
back_log = 300
 
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
 
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = master-log
binlog_format = mixed
expire_logs_days = 10
slow_query_log = 1
long_query_time = 1
performance_schema = 0
explicit_defaults_for_timestamp
 
lower_case_table_names = 1
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
 
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
 
interactive_timeout = 28800
wait_timeout = 28800
 
[mysqldump]
quick
max_allowed_packet = 16M
 
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
 

修改配置後重啓 MySQL

service mysqld restart

③訪問 MySQL

mysql -uusername -ppassword

創建用戶

MySQL 數據庫中,爲不存在的用戶授權,就是同步創建用戶並授權. 此用戶是從庫訪問主庫使用的用戶 ,ip 地址不能寫爲%. 因爲主從備份中,當前創建的用戶,是給從庫 Slave 訪問主庫 Master 使用的.用戶必須有指定的訪問地址.不能是通用地址.

grant all privileges on *.* to ‘username’@’ip’ identified by ‘password’ with grant option;

flush privileges;

查看用戶

use mysql;
select host, user from user;

查看 Master 信息

show master status;

配置從節點

修改 Slave 配置文件 /etc/my.cnf

server_id 唯一標識, 本環境中配置爲 : 2

log_bin  可以使用默認配置, 也可以註釋

重啓 MySQL 服務

service mysqld restart 

訪問 mysql

mysql -uusername -ppassword

停止 Slave 功能

stop slave

配置主庫信息

需要修改的數據是依據 Master 信息修改的. ip Master 所在物理機 IP. 用戶名和密碼是 Master 提供的 Slave 訪問用戶名和密碼. 日誌文件是在 Master 中查看的主庫信息提供的.在 Master 中使用命令 show master status 查看日誌文件名稱.

change master to master_host=’ip’, master_user=’username’, master_password=’password’,
master_log_file=’log_file_name’;
-- 啓動slave
start slave;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章