zoukankan      html  css  js  c++  java
  • MySQL-Jira双机热备

    主服务器:192.168.1.23

    从服务器:192.168.1.243

    一、主服务器Master配置

    1. 创建同步账号、赋权

    在主服务器上为从服务器建立一个连接帐户,此处用root,该帐户必须授予REPLICATION SLAVE权限。因为从mysql版本3.2以后就可以通过REPLICATION对其进行双机热备的功能操作。

    mysql> grant replication slave on *.* to 'root'@'192.168.1.243' identified by '123456';
    mysql> flush privileges;

    创建好同步连接帐户后,我们可以通过在从服务器(Slave)上用replicat帐户对主服务器(Master)数据库进行访问下,看下是否能连接成功。

    在从服务器(Slave)上输入如下指令:

    [root@ha-db ~]# mysql -h192.168.1.23 -uroot -p123456

    2. 修改Mysql配置文件

    找到mysql配置所有在目录,一般在安装好mysql服务后,都会将配置文件复制一一份出来放到/ect目录下面,并且配置文件命名为:my.cnf。即配置文件准确目录为/etc/my.cnf

    项目里的mysql配置文件在/opt/product/mysql目录下。

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

    3. 重启mysql服务

    /etc/init.d/mysqld restart

    4. 查看主服务器状态

    mysql> flush tables with read lock;
    mysql> show master status G;
    *************************** 1. row ***************************
                 File: mysql-bin.000002
             Position: 1290
         Binlog_Do_DB: jira
     Binlog_Ignore_DB: mysql
    Executed_Gtid_Set:
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified

    注意看里面的参数,特别前面两个File和Position,在从服务器(Slave)配置主从关系会有用到的。

    注:这里使用了锁表,目的是为了产生环境中不让进新的数据,好让从服务器定位同步位置,初次同步完成后,记得解锁。

    mysql> unlock tables;

    二、从服务器Slave配置

    1.修改配置文件

    因为这里面是以主-从方式实现mysql双机热备的,所以在从服务器就不用在建立同步帐户了,直接打开配置文件my.cnf进行修改即可,道理还是同修改主服务器上的一样,只不过需要修改的参数不一样而已。如下:

    [mysqld]
    server-id = 2
    log-bin=mysql-bin
    replicate-do-db = jira
    replicate-ignore-db = mysql,information_schema,performance_schema

    2.重启mysql服务

    修改完配置文件后,保存后,重启一下mysql服务,如果成功则没问题。

    3.用change mster 语句指定同步位置

    这步是最关键的一步了,在进入mysql操作界面后,输入如下指令:

    //先停步slave服务线程,这个是很重要的,如果不这样做会造成以下操作不成功。
    mysql>stop slave;          
    mysql>change master to master_host='192.168.1.23',master_user='root',master_password='123456',master_log_file=' mysql-bin.000002 ',master_log_pos=1290;
    mysql>start slave;

    注:master_log_file, master_log_pos由主服务器(Master)查出的状态值中确定。也就是上面刚刚叫注意的。master_log_file对应File, master_log_pos对应Position。Mysql 5.x以上版本已经不支持在配置文件中指定主服务器相关选项。

    遇到的问题,如果按上面步骤之后还出现如下情况:

    4.查看从服务器(Slave)状态

    mysql> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.14
                      Master_User: root
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000002
              Read_Master_Log_Pos: 1290
                   Relay_Log_File: ha-db-relay-bin.000002
                    Relay_Log_Pos: 1453
            Relay_Master_Log_File: mysql-bin.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: jira
              Replicate_Ignore_DB: mysql,information_schema,performance_schema
               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: 1290
                  Relay_Log_Space: 1626
                  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: 08337b95-6e04-11e7-a595-000c2994f6ec
                 Master_Info_File: /opt/product/mysql/data/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
               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
    1 row in set (0.00 sec)
    
    ERROR:
    No query specified

    查看下面两项值均为Yes,即表示设置从服务器成功。

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    三、测试同步

    参考:http://blog.csdn.net/huaweitman/article/details/50853075

    过程出现的问题:http://bbs.csdn.net/topics/360198840

  • 相关阅读:
    洛谷3703 [SDOI2017] 树点染色 【LCT】【线段树】
    BZOJ4818 [SDOI2017] 序列计数 【矩阵快速幂】
    HDU4625 JZPTREE 【树形DP】【第二类斯特林数】
    LOJ2116 [HNOI2015] 开店 【点分治】
    [NOIP2017] 逛公园 【最短路】【强连通分量】
    css
    html
    spring-springmvc-jdbc小案例
    eclipse myeclipse中的一些配置
    springmvc中的一些服务器报错
  • 原文地址:https://www.cnblogs.com/zihanxing/p/7307973.html
Copyright © 2011-2022 走看看