mysql半主從複製

來源:互聯網
上載者:User

標籤:mysql   半主從複製   

mysql半主從複製

node3: 172.16.92.3/16 mariadb主伺服器
node4: 172.16.92.4/16 mariadb從伺服器
以上節點均為CentOS 7.1

配置環境
1. 配置好光碟片yum源
2. 關閉selinux和iptables


node3: mariadb主伺服器

[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#######以下的內容為添加########
#二進位變更日誌
log-bin=mysql-bin
#二進位日誌格式為混合模式
binlog_format=mixed
#為主伺服器node3的ID值
server-id = 1
port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###############################

###### 以下的內容可選 ########
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
#############################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
############### End for my.cnf #################

[[email protected] ~]# rpm -ql mariadb-server | grep ‘semisync‘
/usr/lib64/mysql/plugin/semisync_master.so    #用於Master伺服器安裝的半同步外掛程式
/usr/lib64/mysql/plugin/semisync_slave.so        #用於Slave伺服器安裝的半同步外掛程式

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql
MariaDB [(none)]> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘;     #安裝Master半同步外掛程式
MariaDB [(none)]> set global rpl_semi_sync_master_enabled = 1;    #開啟Master半同步功能
MariaDB [(none)]> set global rpl_semi_sync_master_timeout = 2000;    #逾時時間毫秒級
MariaDB [(none)]> grant replication client,replication slave on *.* to ‘repluser‘@‘172.16.92.4‘ identified by ‘replpass‘;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 497
    Binlog_Do_DB:
Binlog_Ignore_DB:

##### 記下mysql-bin.000003 和 497 , 設定從伺服器中繼日誌時有用 ####



node4: mariadb從伺服器

[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

########## 添加以下內容 ##########
log-bin=mysql-bin
binlog_format=mixed
server-id = 2
relay-log = relay-bin
log_slave_updates = 1
read_only = on

port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###################################

######### 以下內容可選 ############
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
####################################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

############# End of my.cnf ###############

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql
MariaDB [(none)]> show global variables like ‘%read_only%‘\G
*************************** 1. row ***************************
Variable_name: read_only
        Value: ON

MariaDB [(none)]> change master to master_host=‘172.16.92.3‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=497,master_connect_retry=5,master_heartbeat_period=2;
MariaDB [(none)]> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;    #安裝Slave半同步外掛程式
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = 1;        #開啟Slave半同步功能
MariaDB [(none)]> start slave;        #重啟IO線程生效

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
    ........ 其餘資訊略 ........

MariaDB [(none)]> show processlist\G
*************************** 3. row ***************************
   State: Slave has read all relay log; waiting for the slave I/O thread to update it
    ........ 其餘資訊略 ........說明: 從節點已經接收到所有的中繼日誌

MariaDB [(none)]> show global status like ‘%semi%‘;
| Rpl_semi_sync_slave_status | ON |#半同步狀態已經開啟了;

MariaDB [(none)]> show global variables like ‘%rpl%‘;
| rpl_semi_sync_slave_enabled | ON |#從伺服器節點的半同步已經開啟;


node3 主節點上可查看到此進程
MariaDB [(none)]> show processlist\G
*************************** 2. row ***************************
   State: Master has sent all binlog to slave; waiting for binlog to be updated
    ........ 其餘資訊略 ........

MariaDB [(none)]> show global status like ‘rpl_semi%‘;
| Rpl_semi_sync_master_clients | 1 |    #可以看出有一個半同步用戶端了;
    ........ 其餘資訊略 ........

在主節點上建立資料庫測試是否能主從半同步
MariaDB [(none)]> create database testdb;

在從節點上可看到testdb資料庫, 說明主從同步成功!
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+

再來看一下 從節點 的狀態
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.3
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 584        #497->584
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 616            #529->616
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes


############# mysql半主從複製結束 ##############

本文出自 “8317626” 部落格,請務必保留此出處http://8327626.blog.51cto.com/8317626/1711198

mysql半主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.