MySql教程

MySQL 主从同步配置

本文主要是介绍MySQL 主从同步配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、主从同步配置

主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave)。因为复制是异步进行的,所以从服务器不需要一直连接着主服务器,从服务器甚至可以通过拨号断断续续地连接主服务器。通过配置文件,可以指定复制所有的数据库,某个数据库,甚至是某个数据库上的某个表。

使用主从同步的好处:通过增加从服务器来提高数据库的性能,在主服务器上执行写入和更新,在从服务器上向外提供读功能,可以动态地调整从服务器的数量,从而调整整个数据库的性能。提高数据安全,因为数据已复制到从服务器,从服务器可以终止复制进程,所以,可以在从服务器上备份而不破坏主服务器相应数据。在主服务器上生成实时数据,而在从服务器上分析这些数据,从而提高主服务器的性能.

 二、环境

两个干净的虚拟机里面都有MySQL。

三、主节点配置

 

 配置完成之后进入主配置的 MySQL 。

grant replication slave on *.* to '+用户名'@+用户IP identified by '+密码';            //添加一个用户给他权限
flush privileges;                                                                   //刷新权限
show master status ;                                                            //查看server节点状态

 四、从节点配置

 进人第二个虚拟机,直接进入 MySQL 就行。

mysql> change master to master_host='192.168.146.137',master_user='guoming',master_password='222222',master_log_file='gm.000002',master_log_pos=414;        //创建连接, 主节点信息,bin_log文件名及大小
Query OK, 0 rows affected, 2 warnings (0.00 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.146.137
                  Master_User: guoming
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: gm.000002
          Read_Master_Log_Pos: 414
               Relay_Log_File: 192-relay-bin.000002
                Relay_Log_Pos: 276
        Relay_Master_Log_File: gm.000002

       //这俩个是yes 就可以了
             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: 414
              Relay_Log_Space: 447
              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: 137
                  Master_UUID: ebc6cb1d-1ce0-11ec-af2b-000c29efc887
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql>                 

 五、测试

主节点创建一个库,然后再到从节点查看这个库是否存在。

主节点创建的库

从节点也可以查到这个库。

 

 

 



这篇关于MySQL 主从同步配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!