当前位置:Gxlcms > 数据库问题 > MySQL配置主从备份

MySQL配置主从备份

时间:2021-07-01 10:21:17 帮助过:31人阅读

>flush tables with read lock;

2、使用mysqldump工具导出数据:

mysqldump -uroot -pxxx database_name >database_name.sql

3、备份完成后,解锁数据库:

>unlock tables;

4、将初始数据导入从数据库:

>create database database_name;
>use database_name;
>source database_name.sql;

 完成以上操作后,主从服务器就有一样的初态了。

三、主从同步设置:

1、配置从数据库:

/etc/my.cnf主要配置如下:

log-bin=mysql-bin                                 #开启二进制日志
    

server-id       = 2                               #主数据库id为1,不能相同。
replicate_wild_do_table=test.%                    #只同步test库下的表
relay_log=mysqld-relay-bin                        #记录中继日志
log-slave-updates=YES                             #从服务器同步后记录日志

修改完成后重启mysql服务。

2、查看主服务器日记记录位置:

>show master status\G

显示内容如下:

***************** 1. row ****************
            File: mysql-bin.000001       #当前记录的日志
        Position: 80647293               #日志中记录的位置
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

3、主服务器创建允许从服务器同步数据的账户:

>grant replication slave on *.* to user_name@192.168.0.226 identified by ahaii;

4、从服务器开启同步:

>change master to     
              master_host=192.168.0.225,               
              master_user=user_name,    
              master_password=ahaii,   
              master_log_file=mysql-bin.000001,    
              master_log_pos=80647293;

配置完以上后,重启从服务器mysql服务。

5、查看从服务器是否已经成功开启同步:

>show slave status\G

显示如下:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.225
                  Master_User: user_name
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1114
               Relay_Log_File: mysqld-relay-bin.000004
                Relay_Log_Pos: 1260
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: test.%
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1114
              Relay_Log_Space: 1563
              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)

其中:Slave_IO_Running和Slave_SQL_Running的状态都是YES,说明同步开启成功。

现在就可以去主服务器上的test库下创建表开测试同步了。

MySQL配置主从备份

标签:count   安装配置   pac   space   主从备份   hang   日记   mysqldump   pat   

人气教程排行