MariaDB主从配置

实施流程:

相互加秘钥

统一hosts文件:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.75  bj02-mha-01  mha1
192.168.1.76  bj02-mha-02  mha2
192.168.1.77  bj02-mha-03  mha3

添加yum源:

cat /etc/yum.repos.d/MariaDB.repo 
# MariaDB 10.1 CentOS repository list - created 2016-08-29 03:01 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装:

yum install MariaDB-server MariaDB-client -y

service mysql start

chkconfig mysql on

主从配置:

主:mha1
从:mha2、mha3

三台执行:

    create database mha;
    grant all privileges on *.* to mysql@'%'  IDENTIFIED BY 'mysql';
    flush privileges;


三台配置:

    [root@bj02-mha-01 ~]# cat /etc/my.cnf

    [mysqld]
    server-id=1
    log-bin=mysql-bin 
    binlog-do-db=mha


    [root@bj02-mha-02 ~]# cat /etc/my.cnf

    [mysqld]
    server-id=2
    log-bin=mysql-bin
    binlog-do-db=mha


    [root@bj02-mha-03 ~]# cat /etc/my.cnf

    [mysqld]
    server-id=3
    log-bin=mysql-bin
    binlog-do-db=mha

三台重启mysql


主:

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 313
    Binlog_Do_DB: mha
Binlog_Ignore_DB: 
1 row in set (0.00 sec)


从:

MariaDB [(none)]> change master to master_host='mha1',master_user='mysql',master_password='mysql',master_log_file='mysql-bin.000001',master_log_pos=313;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mha1
                  Master_User: mysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 313
               Relay_Log_File: bj02-mha-02-relay-bin.000002
                Relay_Log_Pos: 537
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes                  #此处必须为yes
            Slave_SQL_Running: Yes                  #此处必须为yes

            ...

测试:
    主库操作:
        MariaDB [(none)]> use mha;
        MariaDB [mysqltest]> create table user(id int(5),name char(10));
        MariaDB [mysqltest]> insert into user values (00001,'zhangsan');
    两个从库查看:
        MariaDB [(none)]> use mha;

        MariaDB [mha]> select * from user;
        +------+----------+
        | id   | name     |
        +------+----------+
        |    1 | zhangsan |
        +------+----------+
        1 row in set (0.00 sec)
posted @ 2022-04-22 23:52  忱康  阅读(388)  评论(0)    收藏  举报