mysql 本地 主从配置

1、首先要在本地建立两个mysql服务,指定不同的端口。我
这里一个主(3306),一个从(3307)。

2、
然后修改主配置文件:

[mysqld]

#不能和从一样
server-id = 1   

#要同步的数据库
binlog-do-db=test 

 #不同步的数据库,如果指定了binlog-do-db这里应该可以不用指定的
binlog-ignore-db=mysql  

 #要生成的二进制日记文件名称
log-bin=mysql-bin

修改从配置文件:

[mysqld]

server-id = 2

log-bin    = mysql-bin

replicate-do-db=test

3、在主库添加一个用户 repl 并指定replication权限

create user 'repl'@'127.0.0.1' identified by 'repl';

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1'; -- --这里我指定数据库(test.*)时报错,而指定全库(*.*)时会成功。

4、在主数据库里面运行show master status;记下file和position字段对应的参数

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 | test         | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5、在从库设置它的master:

mysql> change master to master_host='127.0.0.1',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000001',master_log_pos=154;

Query OK, 0 rows affected (0.19 sec)

这里的master_log_file和master_log_pos对应刚才show master status记下的参数

6、在从库开启从数据库复制功能


mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

在从库可以通过 show slave status\G 来查看一些参数

Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: ÃÎ-relay-bin.000017
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 154
              Relay_Log_Space: 737
              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
                  Master_UUID: 6391a55a-3f41-11e9-9360-00ffcca3f8da
             Master_Info_File: D:\phpstudy_pro\Extensions\MySQL5.7.26\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:
     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 Slave_SQL_Running 都是yes 证明成功了,就可以在主库经行操作了

7、mysql 在命令行的一些操作

1、 显示数据库列表。

show databases;

缺省有两个数据库:mysql和test。 mysql库存放着mysql的系统和用户权限信息,我们改密码和新增用户,实际上就是对这个库进行操作。

2、 显示库中的数据表:

use mysql;

show tables;

3、 显示数据表的结构:

describe 表名;

4、 建库与删库:

create database 库名;

drop database 库名;

5、 建表:
//选择数据库
use 库名;

create table 表名(字段列表);

drop table 表名;

6、 清空表中记录:

delete from 表名;

7、 显示表中的记录:

select * from 表名;

8、经常遇见的问题

(一、)如果是 ** Slave_SQL_Running:no**
解决办法如下:

mysql> stop slave;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;
mysql> start slave;
mysql> show slave status\G

(二、)如果是 ** Slave_IO_Running: no**
解决办法如下:

查看主库 show master status\G

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

查看从库 show slave status\G

      Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: ÃÎ-relay-bin.000017
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000002
看其中主库的File   和 从库的   Master_Log_File 是否一致

如果不一致
执行以下操作
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=0;  
mysql> start slave;
mysql> show slave status
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值