mariadb主从配置(一主一从)

master设置:

  • 开启二进制日志
  • 授权同步二进制文件的用户

slave设置:

  • 设置只读
  • 使用change master to将角色转化为slave

master配置文件添加如下,开启二进制日志
[mysqld]
server_id=1
log_bin
授权同步二进制文件的用户
MariaDB [(none)]>grant replication slave on *.* to 'reliuser'@'10.35.78.%' identified by 'tongbu';
MariaDB [(none)]> show master status; #查看最新binlog列表   # 或者show master logs #查看binlog列表
+-----------------------+----------+--------------+------------------+
| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------------+----------+--------------+------------------+
| mariadb-bin.000001    |      400 |              |                  |
+-----------------------+----------+--------------+------------------+
因为 grant 操作mysql.user表,因此二进制日志也会记录grant,Position 从开始的245增长到400

MariaDB [(none)]> show binlog events in 'mariadb-bin.000001';
+--------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| Log_name           | Pos | Event_type  | Server_id | End_log_pos | Info                                                                             |
+--------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| mariadb-bin.000001 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.68-MariaDB, Binlog ver: 4                                        |
| mariadb-bin.000001 | 245 | Query       |         1 |         400 | grant replication slave on *.* to 'reliuser'@'10.35.78.%' identified by 'tongbu' |
+--------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
2 rows in set (0.01 sec)

MariaDB [(none)]> select user,password from mysql.user;

……
|          |                                           |
| reliuser | *69C3B9CD6B3F5BED3AD2E8030541CD815A668188 |
+----------+-------------------------------------------+


ps:
二进制日志只会记录写操作(包括删除),查询什么的不会记录
二进制日志只会记录操作成功的,失败的不会记录

slave配置文件增添如下:

[mysqld]
server_id=2
read_only=ON	#(主到从是单向同步,从数据库设置只读,防止数据写入从服务器,主从数据不一致问题,但是mysql的root仍旧可以写入)

MariaDB [power]> help change master to;

CHANGE MASTER TO
  MASTER_HOST='mysql-1', ###MASTER_HOST尽量不设置ip,使用域名并在主机上设置dns
  MASTER_USER='reliuser',
  MASTER_PASSWORD='tongbu',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mariadb-bin.000001',
  MASTER_LOG_POS=400,
  MASTER_CONNECT_RETRY=10;
MariaDB [power]> start slave;
MariaDB [power]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-1
                  Master_User: reliuser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 400
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             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: 400
              Relay_Log_Space: 827
              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)
--------------------------------------------------------------------master--------------------------------------
MariaDB [(none)]> show processlist;
+----+----------+---------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| Id | User     | Host          | db   | Command     | Time | State                                                                 | Info             | Progress |
+----+----------+---------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
|  2 | root     | localhost     | NULL | Query       |    0 | NULL                                                                  | show processlist |    0.000 |
|  3 | reliuser | mysql-2:57936 | NULL | Binlog Dump |   91 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |    0.000 |
+----+----------+---------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
2 rows in set (0.02 sec)

--------------------------------------------------------------------slave--------------------------------------
MariaDB [power]> show processlist;
+----+-------------+-----------+-------+---------+------+-----------------------------------------------------------------------------+------------------+----------+
| Id | User        | Host      | db    | Command | Time | State                                                                       | Info             | Progress |
+----+-------------+-----------+-------+---------+------+-----------------------------------------------------------------------------+------------------+----------+
|  2 | system user |           | NULL  | Connect |   91 | Waiting for master to send event                                            | NULL             |    0.000 |----->	Slave_IO
|  1 | system user |           | NULL  | Connect | 2103 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |    0.000 |----->	Slave_SQL_Running
|  4 | root        | localhost | power | Query   |    0 | NULL                                                                        | show processlist |    0.000 |
+----+-------------+-----------+-------+---------+------+-----------------------------------------------------------------------------+------------------+----------+
3 rows in set (0.00 sec)

验证主从复制

master:

MariaDB [(none)]> use test;
CREATE TABLE `student`(
	`s_id` VARCHAR(20),
	`s_name` VARCHAR(20) NOT NULL DEFAULT '',
	`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
	`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
	PRIMARY KEY(`s_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `course`(
	`c_id`  VARCHAR(20),
	`c_name` VARCHAR(20) NOT NULL DEFAULT '',
	`t_id` VARCHAR(20) NOT NULL,
	PRIMARY KEY(`c_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `teacher`(
	`t_id` VARCHAR(20),
	`t_name` VARCHAR(20) NOT NULL DEFAULT '',
	PRIMARY KEY(`t_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `score`(
	`s_id` VARCHAR(20),
	`c_id`  VARCHAR(20),
	`s_score` INT(3),
	PRIMARY KEY(`s_id`,`c_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into student values('01' , '赵雷' , '1990-01-01' , '男');
insert into student values('02' , '钱电' , '1990-12-21' , '男');
insert into student values('03' , '孙风' , '1990-05-20' , '男');
insert into student values('04' , '李云' , '1990-08-06' , '男');
insert into student values('05' , '周梅' , '1991-12-01' , '女');
insert into student values('06' , '吴兰' , '1992-03-01' , '女');
insert into student values('07' , '郑竹' , '1989-07-01' , '女');
insert into student values('08' , '王菊' , '1990-01-20' , '女');

insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '03');

insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');


insert into score values('01' , '01' , 80);
insert into score values('01' , '02' , 90);
insert into score values('01' , '03' , 99);
insert into score values('02' , '01' , 70);
insert into score values('02' , '02' , 60);
insert into score values('02' , '03' , 80);
insert into score values('03' , '01' , 80);
insert into score values('03' , '02' , 80);
insert into score values('03' , '03' , 80);
insert into score values('04' , '01' , 50);
insert into score values('04' , '02' , 30);
insert into score values('04' , '03' , 20);
insert into score values('05' , '01' , 76);
insert into score values('05' , '02' , 87);
insert into score values('06' , '01' , 31);
insert into score values('06' , '03' , 34);
insert into score values('07' , '02' , 89);
insert into score values('07' , '03' , 98);

binlog日志查看与备份策略
mysqlbinlog mariadb-bin.000001 >dump.sql #binlog转化为可行性的sql语句,因此binlog从
某种意义上也是数据库的一种备份,建议备份在不同的磁盘或分区,可以在/etc/my.cnf中配置log-bin=/path/
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值