MySQL笔记-简单配置主从库

这里以2台MySQL为例进行实验:

Master库:

ip:192.168.79.134

Slave库:

ip:192.168.79.136

主库修改或增加/etc/my.cnf为:

[mysqld]
server_id=1
log-bin=mysql-bin

从库增加或修改/etc/my.cnf

[mysqld]
server-id=2

然后重启主库及从库

service mysqld restart

在主库中增加帐号并授予权限:

CREATE USER 'rep1'@'192.168.79.136' IDENTIFIED BY 'rep1';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.79.136';
flush privileges;

在主库中查看状态:

sql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000042 |      966 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从中可以看到主库中Master中对应的binlog为binlog.000042,其中position为996。

随后在从库中进行配置

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.79.134',
    -> MASTER_USER='rep1',
    -> MASTER_PASSWORD='rep1',
    -> MASTER_LOG_FILE='binlog.000042',
    -> MASTER_LOG_POS=966;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

在从库中进行开启:

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

在从库中查看从库状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.79.134
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000042
          Read_Master_Log_Pos: 966
               Relay_Log_File: relaylog.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: binlog.000042
             Slave_IO_Running: Connecting
            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: 966
              Relay_Log_Space: 154
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /u01/mysql3306/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: 200606 09:23:24
     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为Connecting及Slave_SQL_Running为Yes就说明成功了。

下面来测试下:

在主库(192.168.79.134)中添加一个新database

mysql> create database mydatabase;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    5
Current database: *** NONE ***

Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

从库(192.168.79.136)中可以查看已经同步了主库(192.168.79.134)中的数据:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
5 rows in set (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/106583893
今日推荐