MySQL主从配置
读写分离:写数据到主库,读数据到从库,从库会自动同步主库的数据
复制原有mysql,重命名并修改my.ini
主库my.ini
[mysqld]
#开启日志
log_bin = mysql-bin
#屏蔽系统库同步
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
#设置需要同步的数据库
binlog-do-db = user_db
binlog-do-db = store_db
binlog-do-db = product_db_1
binlog-do-db = product_db_2
#路径
basedir = "D:mysqlmysql-5.7.25-winx64"
datadir = "D:mysqlmysql-5.7.25-winx64data"
#端口
port = 3306
#设置服务id, 主从不能一致
server_id = 1
从库my.ini
[mysqld]
#开启日志
log_bin = mysql-bin
#屏蔽系统库同步
binlog-ignore-db = mysql.%
binlog-ignore-db = information_schema.%
binlog-ignore-db = performance_schema.%
#设置需要同步的数据库
replicate_wild_do_table = user_db.%
replicate_wild_do_table = store_db.%
replicate_wild_do_table = product_db_1.%
replicate_wild_do_table = product_db_2.%
#路径
basedir = D:mysqlmysql-5.7.25-winx64-s1
datadir = D:mysqlmysql-5.7.25-winx64-s1data
#端口
port = 3307
# 设置服务id, 主从不能一致
server_id = 2
管理员模式启动cmd(不用管理员会报没有权限错误mysql Install/Remove of the Service Denied!),把从库安装为windows服务
D:mysqlmysql-5.7.25-winx64-s1in>mysqld install mysqls1 ‐‐defaults‐file="D:mysqlmysql-5.7.25-winx64-s1my.ini"
由于从库是直接复制的,还需要把从库中的D:mysqlmysql-5.7.25-winx64-s1dataauto.cnf删掉(不删会导致主从同步失败),保证主从的server-uuid不同
重启主库MySQL服务和从库mysqls1服务,会自动生成从库的auto.cnf
授权主从同步专用账号,在主库下运行如下语句
#刷新权限(此操作若在GRANT后会报错,ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot exe)
FLUSH PRIVILEGES;
#授权主备复制专用账号
GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%' IDENTIFIED BY 'db_sync';
#确认位点 记录下文件名File字段值以及位点Position值
show master status;
设置从库向主库同步数据、并检查链路,在从库下运行如下语句
#停止同步
STOP SLAVE;
#修改从库指向到主库,使用上一步记录的文件名以及位点
CHANGE MASTER TO
master_host = 'localhost',
master_user = 'db_sync',
master_password = 'db_sync',
master_log_file = 'mysql-bin.000004',
master_log_pos = 26752;
#开启同步
START SLAVE;
#查看从库状态Slave_IO_Runing和Slave_SQL_Runing都为Yes说明同步成功,如果不为Yes,请检查error_log,然后排查相关异常
SHOW SLAVE STATUS;
最后测试在主库修改数据库,看从库是否能够同步成功,比如修改一条记录