1.修改主服务器配置文件
vi /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1 |
改完 重启动mysql
2. 创建一个账户
mysql > grant replication slave on *.* to 'rep'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.01 sec) |
replication 表示这个账户只能用来进行主从复制
3. 获取二进制日志的信息并导出数据库,步骤:
刷新所有的表,同时给数据库加上一把锁,阻止对数据库进行任何的写操作
mysql > flush tables with read lock; |
warning:这一步将导致数据库短期无法写入任何数据。
获取二进制日志的信息
mysql > show master status; |
+------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 106 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) |
File的值是当前使用的二进制日志的文件名,Position是该日志里面的位置信息(不需要纠结这个究竟代表什么),记住这两个值,会在下面配置从服务器时用到。
mkdir /backup/sql2020 mysqldump -uroot -p'123456' --all-databases > /backup/sql2020/mysql_bak.$(date +%F).sql |
4. 解锁MYSQL数据库
mysql > unlock tables; |
解锁,所有数据库可以使用了
5. 配置从服务器
my.cnf
[mysqld] server-id=2 |
如果有多个从服务器上,那么每个服务器上配置的server-id都必须不一致。从服务器上没必要配置log-bin,除非某一天从服务器可能要作为主来用。可以反过来复制
6. 把主服务器的sql文件,导入从服务器
如果主服务器导出了数据,下面就导入该文件,如果主服务器没有数据,就忽略这一步
把主服务器导出来的文件,拉到从服务器上,进行导入
这个操作在从服务器上也建立了文件夹/backup/sql2020
mysql -uroot -p'123456' < /backup/sql2020/mysql_bak.2020-05-08.sql |
7. 在从服务器里设置同步
mysql> CHANGE MASTER TO MASTER_HOST='47.95.209.111', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106; |
解释一下:
MASTER_HOST 主服务器IP
MASTER_USER 主服务器账户
MASTER_LOG_FILE 主服务器查出来的那个文件
MASTER_LOG_POS=106; 主服务器查出来的那个文件的位置
启动
mysql > start slave; |
8. 检查同步是否成功
mysql > show slave status \G; |
Slave_IO_State: Waiting for master to send event Master_Host: 47.95.209.111 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 32295 Relay_Log_File: dy-relay-bin.000002 Relay_Log_Pos: 303 Relay_Master_Log_File: mysql-bin.000001 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: 106 Relay_Log_Space: 32696 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: Master_Info_File: /var/lib/mysql/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: 1 row in set (0.00 sec) |
Slave_IO_Running: Yes Slave_SQL_Running: Yes |
看到这两个都是Yes 就说明成功了。
我在主服务器插入一条新数据,从服务器不到5秒就得到了。很牛
有两种方式,
1.在主库上指定主库二进制日志记录的库或忽略的库:
vim /etc/my.cnf
...
binlog-do-db=xxxx 二进制日志记录的数据库
binlog-ignore-db=xxxx 二进制日志中忽略数据库
以上任意指定其中一行参数就行,如果需要忽略多个库,则添加多行
...
重启mysql
2.在从库上指定复制哪些库或者不负责哪些库
#编辑my.cnf,在mysqld字段添加如下内容:
replicate-do-db 设定需要复制的数据库
replicate-ignore-db 设定需要忽略的复制数据库
replicate-do-table 设定需要复制的表
replicate-ignore-table 设定需要忽略的复制表
replicate-wild-do-table 同replication-do-table功能一样,但是可以通配符
replicate-wild-ignore-table 同replication-ignore-table功能一样,但是可以加通配符
//例子,只同步一个库school的一个表user
[mysqld] server-id = 1 log-bin = mysql-bin replicate-do-table = school.user //同步的数据库名.表名 slave-skip-errors = all #跳过所有的错误错误,继续执行复制操作 |
#修改后重启mysql
参考资料:
https://www.cnblogs.com/new-journey/p/11319527.html
https://www.cnblogs.com/kylinlin/p/5258719.html