MySQL 主从服务器的配置步骤

本文详细介绍了如何在Linux环境下配置MySQL主从复制,包括主库的my.cnf配置、授权复制、查看复制状态,以及从库的配置、启动复制过程,最后验证同步效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         环境:
         Master:
               ip 192.168.1.110
               os rhel-5.8
               mysql 5.5.28
         Slave:
               ip 192.168.1.113
               os rhel-5.8
               mysql 5.5.28


        
          
             
             ① 主my.cnf的配置

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin     #打开mysql二进制日志
binlog-do-db=test     #设置二进制日志记录的库
server-id       = 1   #设置mysql_id,主从不能相同
binlog-ignore-db=mysql  #设置二进制日志不记录的库
sync_binlog=1         #设置binlog有更新的时候刷新到磁盘


 

             ② 登入主库

use mysql;
grant replication slave,file on *.* to 'Think'@'192.168.1.113' identified by 'mysql';
flush privileges;


                查看主库状态,并记录File和Position

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000002
        Position: 107
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)


 

             ③ 从my.cnf的配置

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id       = 2
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
slave-net-timeout=10


 

             ④ 从库change master

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='192.168.1.110',master_user='Think',master_password='mysql',
    -> master_log_file='mysql-bin.000002',master_log_pos=107;
Query OK, 0 rows affected (0.10 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.110
                  Master_User: Think
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 107
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 107
              Relay_Log_Space: 413
              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
1 row in set (0.00 sec)


 

             ⑤ 验证同步
                主库:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| bin            |
| servers        |
| water          |
+----------------+
3 rows in set (0.00 sec)

mysql> insert into bin values(1);
Query OK, 1 row affected (0.12 sec)


                从库:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| bin            |
| sales          |
| t              |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from bin;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值