master配置my.ini(windows)/my.cnf(linux)
注意:在该mysqld节点下配置 server-id主机一定要和从机不一样,
log-bin一定要开启后面的的mysql-bin为二进制文件的名
[mysqld]
log-bin=mysql-bin
server-id=1
slave配置my.ini(windows)/my.cnf(linux)
主要配置其他的可以采用默认的配置可以在mysql数据库中查看show variables like '%写要查看的配置%';(relay_log,err_log等)
[mysqld]
server-id=2
log-bin=mysql-bin
主机
1、新建一个可以让从机登录的用户并且给该用户授权
CREATE USER '用户名'@'从机ip' IDENTIFIED WITH mysql_native_password BY '登陆密码';
GRANT REPLICATION SLAVE ON *.* TO '用户名'@'从机ip';
flush privileges;
CREATE USER 'loushu'@'172.16.143.0' IDENTIFIED WITH mysql_native_password BY 'W3N1PZZFeNVudx7V';
GRANT REPLICATION SLAVE ON *.* TO 'loushu'@'172.16.143.0';
flush privileges;
2、查看主机的状态这时可以看到log-bin文件的名还有position需要记录下来一会从机配置需要用
SHOW MASTER STATUS;
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000081 | 230657 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
**记下position+file**
从机
通过命令行登录到MySQL里,手动输入的连接上主机
CHANGE MASTER TO
MASTER_HOST='172.16.89.227',---主机ip
MASTER_USER='loushu',--主机用户
MASTER_PASSWORD='W3N1PZZFeNVudx7V',--主机里面创建的密码
MASTER_LOG_FILE='mysql-bin.000081',---SHOW MASTER STATUS;中File
MASTER_LOG_POS=230657;---SHOW MASTER STATUS;中Position
show slave status\G;-- 查看状态一定要slave_sql_running和slave_io_running同时为yes
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.89.227
Master_User: loushu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000081
Read_Master_Log_Pos: 377112
Relay_Log_File: izbp1hifxppw8kaaene5ejz-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000081
Slave_IO_Running: Yes
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: 377112
Relay_Log_Space: 548
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 0ec25597-5af6-11ea-ad8b-00163e0f1f7c
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set (0.00 sec)
ERROR:
No query specified
不成功的话可以多同步几次
reset slave;
stop slave;
start slave;
当主机从机配置成功后
新建一个数据库新建一张表插入数据可以在从机里面读取到刚才插入的数据