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;

Linux(CentOS7)配置 MySQL5.7 主从备份_第1张图片

 

你可能感兴趣的:(Linux)