一.主数据库-master
1.修改配置文件win是my.ini,linux是my.cnf
[mysqld]
#主库和从库需要不一致,配一个唯一的ID编号
server_id=218
#bin-log地址,打开binlog日志
log-bin=/u02/mysql_log/mysql-bin
#指定的是binlog文件的索引文件,这个文件管理了所有的binlog文件的目录
log-bin-index=mysql-bin.index
#只同步这个数据库
binlog-do-db=dbname
2.重启数据库
win: 启动mysql: net start mysql;停止mysql: net stop mysql;
Linux:停止service mysqld stop;启动service mysqld start;
3.登录mysql执行命令:show master status;
登录命令:mysql:mysql -P 3306 -h 192.168.10.218 -u root -p123456;本地可不用加-h
+------------------+----------+--------------+------------------+-----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-----------------------------------------------+
| mysql-bin.000017 | 1012 | dbname | | | |
+------------------+----------+--------------+------------------+-----------------------------------------------+
以上结果表示binlog日志已开启。
二.从数据库-slave
1.修改配置文件win是my.ini,linux是my.cnf
[mysqld]
#主库和从库需要不一致,配一个唯一的ID编号
server_id=25
2.重启数据库=============
3.创建连接主服务器,提前是主服务器提供的用户名权限要够,登录从mysql。
a.执行命令:stop slave;
b.执行命令:change master to master_host='192.168.10.218',master_user='root',master_password='123456', master_log_file='mysql-bin.000014',master_log_pos=18232;
c.执行命令:start slave;
PS:master_log_file就是主服务器执行show master status;查询出来的结果,对应File字段,master_log_pos对应-->Position
4.执行命令查看状态:show slave status \G;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.218
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 1012
Relay_Log_File: DESKTOP-5JF1C0A-relay-bin.000002
Relay_Log_Pos: 1178
Relay_Master_Log_File: mysql-bin.000017
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: 1012
Relay_Log_Space: 1395
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: 14057
Master_UUID: c4a74d0b-ad17-11ea-a146-e454e8a6793d
Master_Info_File: D:\Soft\mysql-5.7.31\data\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:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如上这两个字段都是Yes表名主从同步成功了。
三.测试
在主数据库修改一个数据,再从数据库查看是否已经同步了呢。
四.在途中遇见的错误和问题总结。
1.Slave_SQL_Running: No,一般下面都会有错误提醒的。可根据错误提示解决。
2.Slave_IO_Running: Yes,Slave_SQL_Running: Yes,都为Yes,但是修改主库数据从库数据没有变化,在同步之前把数据库先创建好,把主库的数据先手动同步到从库中,尽量包吃主从要同步的数据一致,因为你的同步起点是创建连接后更新的数据进行同步,不会通同步之前的数据,有可能导致失败。