主从服务
主服务器为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 root@192.168.1.2:/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 user1@192.168.10.2 identified by "pass"
grant replication slave on *.* user1@192.168.10.2 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)