Mysql之主從復制

Mysql之主從復制

節點一

修改配置文件設置唯一ID開起二進制日志

[root@node1 ~]# vim /etc/my.cnf 增加以下內容
    [mysqld]
    log-bin=master_bin  開起二進制日志
    server_id=1     給主節點一個唯一的ID號
    innodb_file_per_table=on    innodb開起獨立表空間
    skip_name_resolve=on    開啟跳過主機名反解

啟動服務創建有遠程復制權限的賬戶

[root@node1 ~]# service mariadb start
[root@node1 ~]# mysql
MariaDB [(none)]> show global variables like '%log%';   查看二進制日志log_bin是否開啟了
MariaDB [(none)]> show global variables like '%server%';    查看DI號是否為1
MariaDB [(none)]> show master logs; 查看主節點二進制日志的位置,從節點從主節點最后一個日志的位置開始復制
MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'192.168.%.%' identified by 'passwd';     創建并授權一個遠程復制賬號copy密碼為passwd
MariaDB [(none)]> flush privileges; 刷新用戶權限

節點二

修改配置文件設置唯一ID開起中繼日志

[root@node2 ~]# vim /etc/my.cnf
    relay_log=relay_log 開起中繼日志
    relay-log-index=relay-log.index 
    server_id=2     同樣的也需要設置唯一的ID號
    innodb_file_per_table=on
    skip_name_resolve=on

[root@node2 ~]# service mariadb start
[root@node2 ~]# mysql
MariaDB [(none)]> show global variables like '%log%';   查看中繼日志relay_log是否開起
MariaDB [(none)]> show global variables like '%server%';    查看ID號是否為2
主節點為192.168.1.107,遠程復制賬號為copy,密碼為passwd,復制二進制日志的起始位置為000003的245處
MariaDB [(none)]> change master to master_host='192.168.1.107',master_user='copy',master_password='passwd',master_log_file='master_bin.000003',master_log_pos=245;
MariaDB [(none)]> start slave;  啟動從節點復制線程


MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.107
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master_bin.000003
          Read_Master_Log_Pos: 491
               Relay_Log_File: relay_log.000003
                Relay_Log_Pos: 776
        Relay_Master_Log_File: master_bin.000003
             Slave_IO_Running: Yes  這兩項必須為yes
            Slave_SQL_Running: Yes  這兩項必須為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: 491
              Relay_Log_Space: 1064
              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)

注意

如果Slave_IO_Running不為yes的解決辦法

如:ERROR 1201 (HY000)

MariaDB [(none)]> slave stop;   停止從節點
MariaDB [(none)]> reset slave;  重新設置從節點

查找設置有問題的地方重新給從節點授權

MariaDB [(none)]> change master to master_host='192.168.1.107',master_user='copy',master_password='passwd',master_log_file='master_bin.000003',master_log_pos=245;
MariaDB [(none)]> start slave;  啟動從節點
MariaDB [(none)]> show slave status\G;  查看狀態

注意從節點上一定不能進行寫操作

驗證

主節點

MariaDB [(none)]> create database msdb;
MariaDB [msdb]> create table xx (id int(4) not null auto_increment,name varchar(30) not null,primary key(id)) engine=innodb charset=utf8;
MariaDB [msdb]> insert into xx (id,name) values (1,'king');

從節點

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| msdb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> use msdb;
MariaDB [msdb]> show tables;
+----------------+
| Tables_in_msdb |
+----------------+
| xx             |
+----------------+
MariaDB [msdb]> select * from xx;
+----+------+
| id | name |
+----+------+
|  1 | king |
+----+------+

原創文章,作者:N17_信風,如若轉載,請注明出處:http://www.www58058.com/24870

(0)
N17_信風N17_信風
上一篇 2016-07-19 09:12
下一篇 2016-07-19 22:45

相關推薦

  • NET25-第7周作業

    1、創建一個10G分區,并格式為ext4文件系統; (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl; [root@node1 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI o…

    Linux干貨 2017-03-15
  • 文本處理工具

    1, head  默認顯示前十行 -n  +行號 顯示前n行 -行號 顯示前n行 -c 字節數 顯示前n個字節 tail 默認顯示后十行 -n +行號 顯示后n行 – 行號 顯示后n行 -f 動態顯示 cut -d 指定分隔符 -f 選取第幾列 –output-delimiter 指定輸出符 相關的實際操作: a,…

    2017-07-29
  • Nginx配置進階

    目錄 ngx_http_rewrite_module模塊 ngx_http_gzip_module模塊 ngx_http_fastcgi_module模塊 ngx_http_ssl_module模塊 ngx_http_referer_module模塊 ngx_http_rewrite_module模塊 將用戶某一次請求的URI當中的字符串是不是能夠被我們給出…

    Linux干貨 2016-11-05
  • 第二周作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 目錄管理類的命令: mkdir, rmdir mkdir:make directories mkdir [OPTION]… DIRECTORY… -p: 自動按需創建父目錄; -v: verbose,顯示詳細過程; -m MODE:直接給定權限; 注意:路徑…

    Linux干貨 2018-01-17
  • linux用戶管理(一)

    作為一個專業的服務器發行版,CentOS上存在著n多個用戶,作為一個專業的運維工程師,linux的用戶管理和用戶組的管理是做為一個SA必不可少的工作. 下面我們就先來談談linux中的用戶管理: useradd可以在系統中新建一個用戶 用法如下: useradd [選項] 用戶名 常用選項: -u 指定用戶的UID -g 指定用戶的初始組ID -G…

    Linux干貨 2017-04-03
  • clsass 10 shell編程(二)及文件查找及壓縮

    一、選擇執行(條件判斷if) 流程控制 過程式編程語言:     順序執行     選擇執行     循環執行 使用read 命令來接受輸入 使用read 來把輸入值分配給一個或多個shell變量:   &nb…

    Linux干貨 2016-08-18
欧美性久久久久