安装环境:
centos7.6
master:192.168.1.15
slave :192.168.1.14
第一步:先两台机器安装好mysql。我这里安装的是mysql5.7
第二步:配置主从mysql配置文件
master上:
server-id = 1 //数据库 ID 号, 为 1 时表示为
log-bin=mysql-bin //启用二进制日志;
binlog-do-db=master_test//需要同步的二进制数据库名,可以写多个;
binlog-ignore-db=mysql //不同步的二进制数据库名
下面是我贴出的配置:
[mysqld]
user=mysql
server-id = 1
port = 3306
basedir=/data/mysql
datadir=/data/mysql/mysql_data
tmpdir = /tmp
skip-external-locking
skip_name_resolve = 1
lower_case_table_names = 1
max_connections = 8000
max_connect_errors = 8000
explicit_defaults_for_timestamp = 1
max_allowed_packet = 256M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 128M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 1M
read_rnd_buffer_size = 2M
binlog_cache_size = 8M
join_buffer_size = 128M
back_log = 600
explicit_defaults_for_timestamp = false
query_cache_wlock_invalidate = OFF
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 set#############
log_error = /data/mysql/log/mysql.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/slow.log
log_queries_not_using_indexes = 1
long_query_time = 2
expire_logs_days = 7
log-bin=/data/mysql/log/mysql-bin
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

slave配置上:
server-id = 2 //数据库 ID 号
[mysqld]
user=mysql
server-id = 2
port = 3306
basedir=/data/mysql
datadir=/data/mysql/mysql_data
tmpdir = /tmp
skip-external-locking
skip_name_resolve = 1
lower_case_table_names = 1
max_connections = 8000
max_connect_errors = 8000
explicit_defaults_for_timestamp = 1
max_allowed_packet = 256M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 128M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 1M
read_rnd_buffer_size = 2M
binlog_cache_size = 8M
join_buffer_size = 128M
back_log = 600
explicit_defaults_for_timestamp = false
query_cache_wlock_invalidate = OFF
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 set#############
log_error = /data/mysql/log/mysql.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/slow.log
log_queries_not_using_indexes = 1
long_query_time = 2
expire_logs_days = 7
log-bin=/data/mysql/log/mysql-bin
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

第三步:设置主从同步用户
master机器上:
mysql主从部署

GRANT ALL PRIVILEGES ON . TO 'appuser'@'192.168.1.14' IDENTIFIED BY 'test@admin' WITH GRANT OPTION;
flush privileges;

slave机器上:
mysql主从部署
GRANT ALL PRIVILEGES ON . TO 'appuser'@'192.168.1.15' IDENTIFIED BY 'test@admin' WITH GRANT OPTION;
flush privileges;

第四步:
查看master库状态:
show master status;
mysql主从部署_第1张图片

第五步:在slave上执行主库同步命令:
先停止slave:
stop slave;
在执行同步命令:
change master to master_host='192.168.1.15',master_user='appuser',master_password='test@admin',master_log_file='mysql-bin.000002',master_log_pos= 1176;

mysql主从部署

再开启slave:
start slave;

master_log_file
master_log_pos
以上两项都需要在master上查看

以上全部做完后,可以再主库上测试,我们新建一个数据库test

mysql主从部署

slave上面就可以看到从库上数据库已经同步完成

mysql主从部署_第2张图片