mysql 主从复制
主从服务
主服务器为192.168.1.1 从服务器为192.168.1.2
备份主服务器test数据库
/user/local/mysql/bin/mysqldump -uroot -p test -l -F >/root/test.sql
把备份文件拷贝到从服务器
scp /root/test.sql [email protected]:/root/test.sql
然后再从服务器上恢复数据库
/usr/local/mysql/bin/mysql -uroot -p test < /root/test.sql
主服务器
修改/etc/my.cnf
server-id必须是唯一值
开启log-bin
创建授权用户
grant all slave on *.* to [email protected] identified by "pass"
grant replication slave on *.* [email protected] identified by "pass"
select user,host,password from mysql.user 查看数据库里的用户名
修改从服务器/etc/my.cnf
server-id 不能和主相同
master-host = 192.168.1.1
master-user = user1
master-password = abc123,
master-port = 3306
pkill 3306 关闭3306端口
再次启动mysql服务
show status\G 列和行倒转 查看 slave_SQL_Running 和 Slave_IO_Running是否为yes
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: user1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 371
Relay_Log_File: localhost-relay-bin.000006
Relay_Log_Pos: 508
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在主服务器上查看
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 371 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
主从服务器的数据库test表test的数据相同
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
再在主服务器上更新数据
mysql> insert into test values('9');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values('10');
Query OK, 1 row affected (0.00 sec)
直接在从服务器上可以同步了
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
10 rows in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.