linux下主从数据库配置

16 篇文章 0 订阅

1.配置条件:

           两台虚拟机分别安装mariadb,安装教程在:  点击打开链接

            主数据库虚拟机:192.168.0.107

            从数据库虚拟机:192.168.0.109

2.配置前奏

          每个数据库都先建个数据库,这个数据就是要主从的数据库,对主库的增删改都会同步到从数据库的.

            主数据库虚拟机:192.168.0.107 :create database test;

            从数据库虚拟机:192.168.0.109: create database test;

3.配置文件位置

           mysql 在/etc/my.cnf

            mariadb在/etc/my.cnf.d/server.cnf

  4.配置主数据库

        先登录数据库配置一个可以访问日志的用户名和密码以及主机IP,这个是给从数据库来访问用的. 

        这里设置的用户名是repl,密码是123,允许ip是局域网192.168.0.1~192.168.0.255都行.

create user 'repl'@'192.168.0.%' identified by '123';
grant replication slave on *.* to 'repl'@'192.168.0.%';
        然后退出去修改配置文件 
 #vi /etc/my.cnf.d/server.cnf

        在  [mysqld] 下添加下面内容

     [mysqld]
    log-bin=mysql-bin#代表着开启日志
    server-id=1 #代表者id
    binlog-do-db=test #代表着针对的哪个数据库,本教程是针对着test数据库

       保存后重启数据库再进入mysql

        执行:show master status;得到以下结果

 MariaDB [(none)]> show master status
    -> ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      245 | test         |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

    记录住file和position字段内容

4.配置从数据库

    先打开配置文件

    加入:

   

     [mysqld]
    server-id=2 #本机id

    replicate-do-db=test #要复制的数据是test

    然后重启数据库,进入数据库,先关闭slave

 stop slave;

       下面这个sql语句要严格大小写.

 CHANGE MASTER TO
    -> MASTER_HOST='192.168.0.107',   #主数据库的ip
    -> MASTER_USER='repl', #用户名
    -> MASTER_PASSWORD='123',#密码
    -> MASTER_LOG_FILE='mysql-bin.000001',#刚刚记录的那个file字段

    -> MASTER_LOG_POS=245;#刚刚记录的那个position字段

    然后再

 start slave;

    接着查看状态:

  show slave status \G;

    结果是:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.107
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes #这个要yes
            Slave_SQL_Running: Yes #这个要yes 
              Replicate_Do_DB: test
          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: 245
              Relay_Log_Space: 825
              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

5.测试

已经配置完了.开始测试

切换到主数据库:

  use test;

  create table  mytest(id int,age int);

再到从库里

 use test;

 show tables;

就会发现已经有    mytest这个表了.

再对主库的mytest表进行增删改

从数据库也可以做到相应的更改.

这个主从数据库算是配置完成.


        

    


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值