mysql 主从配置

mysql主从配置
确保从数据库与主数据库的数据一样先在主数据库创建所需要同步的库和表

1.查看数据库
show databases;

  2.进入想要配置的库

use test

show tables;

 

 3.同步主库与从库 数据库,数据表信息, 内容可以不一致,但是表需要一致。(最好一致,不然做读写可能数据不准确)

 

 4.创建从库用户信息

 主库创建从库同步主库的账户及权限

mysql> create user 'username'@'主库地址' identified by 'password';

mysql> grant replication slave on *.* to 'username'@'主库地址';
//服务器 最起码需要权限 replication slave mysql
> flush privileges;

 

 5.

修改从库my.cnf

//添加以下内容:
server-id=2 //设置从库的唯一标识符 从的必须比主小
relay-log=mysql-relay-bin //启用中继日志relay log 

6.修改主库my.cnf

//添加以下内容
log-bin=mysql-bin //启用binlog日志
server-id=1 //主数据库服务器唯一标识符 主的必须必从大

 

查看主库的状态
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000008 | 382886192 |              |                  |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)

  查看从库状态

show slave status\G 
mysql> show slave status\G      
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 主库地址
                  Master_User: username
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009  //主库log文件
          Read_Master_Log_Pos: 460351
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 460397
        Relay_Master_Log_File: mysql-bin.000009  //主库log文件
             Slave_IO_Running: Yes   //必须yes 
            Slave_SQL_Running: Yes  //必须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: 460251
              Relay_Log_Space: 460653
              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)

 

从库增加  主从同步

change master to master_host='188.0.0.0', master_user='xxx',master_password='xxx',master_log_file='mysql-bin.000008',master_log_pos=0;

 

 

 

错误

如果是slave_SQL_running:no

解决办法:

mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;

 

 

如果是slave_io_running:no

mysql> slave stop;                 
mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=0;  
mysql> slave start;                               
mysql> show slave status\G  

 

posted @ 2019-09-17 11:32  墨白`Blog  阅读(91)  评论(0)    收藏  举报