MySQL主从服务器配置 最新版

105 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第25天,点击查看活动详情

\

配置主库

my.ini

[mysqld]
//端口号
port=3300
//主库id
server_id=1
bin_log=binlog
//能访问到的数据库
binlog_do_db=db
//禁止访问的数据库
binlog_ignore_db=information_schema,mysql,performance_schema,sys
//用户只读
read_only=off
//管理员只读
super_read_only=off

建立账号

MySQL初始化安装后进入MySQL创建账号

-- 建立账号
create user repl identified with mysql_native_password by 'repl';
-- 加权限
grant replication slave on *.* to repl;
-- 刷新
flush privileges;

查看主库状态

show master status;
+---------------+----------+--------------+------------------+---------------
----+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
Executed_Gtid_Set |
+---------------+----------+--------------+------------------+---------------
----+
| binlog.000005 | 41145 | | |
|
+---------------+----------+--------------+------------------+---------------
----+
1 row in set (0.00 sec)

记录这个binlog 及后面的开始位置

从库配置

my.ini

[mysqld]
// 端口号
port=3301
// id不能相同
server_id=2
bin_log=binlog
read_only=on
super_read_only=off

进入从机服务器mysql后

-- 建立复制源
change replication source to
-- 主库ip
source_host='127.0.0.1',
-- 访问账号
source_user='repl',
-- 密码
source_password='repl',
-- 主库端口
source_port=3300,
-- 主库刚才记录的logbin
source_log_file='binlog.000005',
-- logbin开始位置
source_log_pos=41145;
-- 启动从机
start replica;

查看从机状态

​编辑

 配置错误时

停止从机线程:stop replica;

再重新配置即可

正常从库只进行读操作,主库进行写操作

主库重启后从库需要数秒重连