在主服务器上创建mysql用户创建用户(使用root也可以,但是不建议) , 该用户必须有 REPLICATION SLAVE 权限
创建用户:
CREATE USER 'test1'@'192.168.8.%' IDENTIFIED WITH mysql_native_password by '123456';
赋权:
GRANT REPLICATION SLAVE ON *.* TO 'test1'@'192.168.8.%';
刷新权限:
flush privileges;
在从服务器上测试使用主服务器刚刚创建的账号是否能够正常连接 例如
/usr/local/mysql/bin/mysql -utest1 -p123456 -h192.168.8.113
停止主、从服务器的mysql服务,例如:
/etc/init.d/mysql stop
主服务器必须打开开二进制日志 例如 vi /etc/my.cnf (根据实际路径)
主要是修改配置文件 , 一般在 linux 下安装的 mysql 配置文件是 my.cnf, 在
windwos 下是 my.ini,
修改主服务器配置文件
server-id=1
log-bin= 二进制文件的位置 # 主服务器需打开二进制日志 可以是默认值也 可以创建新目录并且赋予可写权限
例如log-bin=/usr/local/mysql/binlog/bin-log
默认日志文件在mysql安装目录下的var目录下
binlog-do-db=mydata # 需要同步的数据库 默认都同步
#binlog-ignore-db= # 需要忽略的数据库 默认不忽略任何数据库
启动mysql服务 /etc/init.d/mysql start
如果使用了默认的二进制日志配置 需要将原始日志文件删除后再启动mysql服务,启动后可以查看二进制日志目录下是否生成新文件,如果已经生成日志文件说明配置是正确的
配置从服务器
server-id = 2
#replicate-do-db=mydata 可选 默认同步所有的数据库
修改server-uuid,主从服务器的uuid不同,打开auto.cnf,
vi /usr/local/mysql/var/auto.cnf
启动mysql服务 /etc/init.d/mysql start
/etc/init.d/mysql start
进入mysql命令 行执行命令
change master to master_host=’ 主 服务 器 地 址 ‘, master_user=’ 用 户名 ‘,
master_password=’ 密码 ',
master_port= 端口号 ,MASTER_CONNECT_RETRY=10;
例如 :
change master to master_host='192.168.8.113',master_user='test1',master_password='123456',master_port=3306,master_connect_retry=10;
再执行 start slave;
start slave;
在从服务器输入命令
show slave status\G;
查看状态 :
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.16.6
Master_User: slave
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 903
Relay_Log_File: relaylog.000004
…
在主服务器上建立数据库数据表后查看从服务器 , 看是否配置成功
撤销配置 :
进入命令行执行 stop slave;
stop slave;
再执行 reset slave; 即可 .
reset slave;
查看状态时显示:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 101.200.*.*
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000113
Read_Master_Log_Pos: 276925387
Relay_Log_File: mysql-relay.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000113
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 276925387
Relay_Log_Space: 120
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 21
Master_UUID: e4a43da7-5b58-11e5-a12f-00163e003632
Master_Info_File: /home/data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 170204 10:48:06
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified