用mysql-cluster-gpl-7.6.7搭建數據庫集羣

用mysql-cluster-gpl-7.6.7搭建數據庫集羣

前言

當你的業務到達必定的當量,確定須要必定數量的數據庫來負載均衡你的數據庫請求,可是有一個問題就是數據同步,由於負載均衡的前提就是,各個服務器的數據庫是數據同步的。在業務量不大的時候,咱們會使用主從複製的方法實現服務器數據同步,一主多從或者是雙主等,可是雖然進行了讀寫分離,可是對於讀的方法限制仍是比較大,因此解決數據同步的問題就是數據庫集羣的意義。我這裏使用mysql官網提供的mysql-cluster實現集羣。php

mysql cluster中的幾個概念解釋

爲了簡單,我後面簡稱mysql-cluster爲mc。html

1. mc已經包含了mysql,我下載的最新的mc7.6.7,官方說明包含的是mysql版本是7.6.7。因此不須要使用別的msyql的安裝包安裝數據庫。同時注意mysql7.6.7的版本在安裝的命令和配置上面和以前的版本有很大的不一樣,因此網上有不少mc7.6.7以前的版本,所包含的mysql版本不一樣,因此安裝方法不一樣。node

2. 管理節點,mc管理節點負責管理、配置、監控整個集羣。mysql

3. 數據節點,使用內存存放數據,保存進數據節點的數據都會自動複製並存儲到其餘數據節點。linux

4. mysql節點,也叫數據庫節點,和咱們平時使用的mysql相同,做爲數據庫使用。被數據節點訪問。nginx

架構圖及說明

avatar

我實驗中的配置就是如圖所示,由於虛擬機佔用內存較大,只使用了3臺服務器,在實際狀況中最好將數據節點和mysql節點分開。在實際中負載均衡服務還須要作備份,由於萬一負載均衡服務器宕機將會致使全部數據節點都沒法訪問,因此須要對負載均衡服務器備份,有條件的話,分開管理節點和負載均衡器。實驗只實現整個數據庫集羣,負載均衡請參考以前的博客配置便可。sql

在實際搭建過程當中,IP地址有所變化:數據庫

hostname ip 說明
mc.controller 192.168.2.131 管理節點
mc.data1 192.168.2.132 數據節點1,mysql節點1
mc.data2 192.168.2.133 數據節點2,mysql節點2

下載mysql cluster

https://dev.mysql.com/downloads/cluster/vim

修改hosts服務器

192.168.2.131 mc.controller
192.168.2.132 mc.data1
192.168.2.133 mc.data2

#執行如下命令關閉防火牆

[user@mc opt]systemctl stop firewalld && systemctl disable firewalld
[user@mc opt]setenforce 0

#將SELINUX的值改爲disabled
[user@mc opt]vim /etc/selinux/config

SELINUX=disabled

安裝數據和mysql節點

如下的全部操做須要在全部的集羣節點都要進行相同的操做,即mc.data一、mc.data2

[user@mc opt] sudo mkdir -p /opt/mysql-cluster
[user@mc opt] sudo chown -R user:user mysql-cluster/
[user@mc opt] mv ~/mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz /opt/mysql-cluster/
[user@mc opt] cd /opt/mysql-cluster
[user@mc opt]tar -zxvf mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz


[user@mc opt] sudo mv /opt/mysql-cluster/mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64/ /usr/local/mysql
[user@mc opt]cd /usr/local/mysql/bin
[user@mc opt]sudo ./mysqld --initialize --user=user --basedir=/usr/local/mysql

#啓動mysql,修改密碼
[user@mc bin]$ nohup ./mysqld &
[user@mc bin]$ ./mysql -uroot -h 127.0.0.1 -p

#輸入上一步初始化時出現的密碼

alter user user() identified by "你的密碼"; 
grant all privileges  on *.* to root@'%' identified by "你的密碼";
flush privileges;

#將mysqld服務加入開機自啓動項。
#首先須要將scripts/mysql.server服務腳本複製到/etc/init.d/,並重命名爲mysqld。
[user@mc support-files]$ sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld


#添加環境變量
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin

安裝配置管理節點

#新建配置文件而且初始化管理節點
[user@mc opt]# vim /usr/local/mysql/config.ini

下面是配置文件,根據本身的需求修改,首先給出官網的默認配置文件,而後給出個人配置文件,根據我修改的修改便可,別的都可不動。

[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=2    # Number of replicas
DataMemory=80M    # How much memory to allocate for data storage
IndexMemory=18M   # How much memory to allocate for index storage
                  # For DataMemory and IndexMemory, we have used the
                  # default values. Since the "world" database takes up
                  # only about 500KB, this should be more than enough for
                  # this example NDB Cluster setup.
ServerPort=2202   # This the default value; however, you can use any
                  # port that is free for all the hosts in the cluster
                  # Note1: It is recommended that you do not specify the port
                  # number at all and simply allow the default value to be used
                  # instead
                  # Note2: The port was formerly specified using the PortNumber 
                  # TCP parameter; this parameter is no longer available in NDB
                  # Cluster 7.5.

[ndb_mgmd]
# Management process options:
HostName=192.168.0.10           # Hostname or IP address of MGM node
DataDir=/var/lib/mysql-cluster  # Directory for MGM node log files

[ndbd]
# Options for data node "A":
                                # (one [ndbd] section per data node)
HostName=192.168.0.30           # Hostname or IP address
NodeId=2                        # Node ID for this data node
DataDir=/usr/local/mysql/data   # Directory for this data node's data files

[ndbd]
# Options for data node "B":
HostName=192.168.0.40           # Hostname or IP address
NodeId=3                        # Node ID for this data node
DataDir=/usr/local/mysql/data   # Directory for this data node's data files

[mysqld]
# SQL node options:
HostName=192.168.0.20           # Hostname or IP address
                                # (additional mysqld connections can be
                                # specified for this node for various
                                # purposes such as running ndb_restore)

我本身的配置:

[NDBD DEFAULT]
NoOfReplicas=2 #定義在Cluster環境中相同數據的份數,最大爲4
DataMemory=200M #每一個數據節點中給數據分配的內存
IndexMemory=20M #每一個數據節點中給索引分配的內存
MaxNoOfAttributes=20480 #該參數用於設置簇中觸發程序對象的最大數目。該參數的默認值爲768,不修改建表時可能會報708錯誤
[NDB_MGMD]
NodeId=1
#設置管理節點服務器
HostName=mc.controller
DataDir=/usr/local/mysql/data
[NDBD]
NodeId=2
#設置存儲節點服務器(NDB節點)
HostName=mc.data1
DataDir=/usr/local/mysql/data
[NDBD]
NodeId=3
#第二個NDB節點
HostName=mc.data2
DataDir=/usr/local/mysql/data
[MYSQLD]
NodeId=4
HostName=mc.data1
[MYSQLD]
NodeId=5
HostName=mc.data2

使用配置文件初始化管理節點

/usr/local/mysql/bin/ndb_mgmd -f /usr/local/mysql/config.ini --initial

(#--initial:第一次啓動時加上,其它時候不要加,否則會數據清空,除非是在備份、恢復或配置變化後重啓時)

出現MySQL Cluster Management Server mysql-5.7.23 ndb-7.6.7就表示成功了

[user@mc mysql]$ ps -aux |grep ndb_mgmd
user      3500  1.0  0.6 502848  6672 ?        Ssl  16:20   0:00 ./bin/ndb_mgmd -f config.ini --initial
user      3512  0.0  0.0 112720   980 pts/0    R+   16:20   0:00 grep --color=auto ndb_mgmd
[root@mc bin]# ./ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]	2 node(s)
id=2 (not connected, accepting connect from mc.data1)
id=3 (not connected, accepting connect from mc.data2)

[ndb_mgmd(MGM)]	1 node(s)
id=1	@192.168.2.131  (mysql-5.7.23 ndb-7.6.7)

[mysqld(API)]	2 node(s)
id=4 (not connected, accepting connect from mc.data1)
id=5 (not connected, accepting connect from mc.data2)

ndb_mgm> exit
[root@mc bin]#

配置數據和mysql節點

如下的全部操做須要在全部的集羣節點都要進行相同的操做,即mc.data一、mc.data2

修改my.cnf配置文件

[mysqld]
ndbcluster  #使用ndb集羣引擎
ndb-connectstring=192.168.2.131 #指定管理集羣的ip地址,多個以,分隔
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

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

#指定管理集羣的ip地址,多個以,分隔
[mysql_cluster]
ndb-connectstring=192.168.2.131

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

在每臺存儲節點(ndbd)服務器上,若是是第一次啓動ndbd進程的話,必須先執行如下命令:

[user@mc mysql]$ cd /usr/local/mysql/bin
[user@mc mysql]$ ./ndbd --initial

#注意,僅應在首次啓動ndbd時,或在備份/恢復數據或配置文件發生變化後重啓ndbd時使用「--initial」參數。由於該參數會使節點刪除由早期ndbd實例建立的、用於恢復的任何文件,包括用於恢復的日誌文件。若是不是第一次啓動,直接運行以下命令便可

[user@mc mysql]$ cd /usr/local/mysql/bin
[user@mc mysql]$ ./ndbd

啓動sql節點:

service mysqld start

查看集羣狀態

[user@mc bin]$ ./ndb_mgm
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]	2 node(s)
id=2	@192.168.2.132  (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0, *)
id=3	@192.168.2.133  (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0)

[ndb_mgmd(MGM)]	1 node(s)
id=1	@192.168.2.131  (mysql-5.7.23 ndb-7.6.7)

[mysqld(API)]	2 node(s)
id=4	@192.168.2.132  (mysql-5.7.23 ndb-7.6.7)
id=5	@192.168.2.133  (mysql-5.7.23 ndb-7.6.7)

啓動和關閉

啓動mysql集羣。啓動順序爲:管理節點→數據節點→SQL節點。

啓動的命令上面都有,刪去--initial便可

關閉時只須要關閉管理節點,後面的數據節點會同時被關閉,mysql就和原來同樣便可

管理節點關閉命令:ndb_mgm -e shutdown

(執行完成以後管理節點會關閉,數據節點也會關閉,但SQL節點不會,也就是數據庫服務須要手動到每一臺服務器上中止以保證數據同步)

用nginx作負載均衡

nginx配置以下:

#user  nobody;
worker_processes  2;

error_log  /logs/error.log warn;  
pid        /logs/nginx.pid;  


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;  
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
	
	autoindex  on;

    #gzip  on;

    server {
        listen       80;
        server_name  192.168.2.169;

        #charset koi8-r;

        #access_log  /wwwlogs/access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

stream {
    upstream mysql {
        server 192.168.2.132:3306 max_fails=2 fail_timeout=10s;
        server 192.168.2.133:3306 max_fails=2 fail_timeout=10s;
    }
 
    server {
		listen 3306;
        proxy_pass mysql;
		proxy_connect_timeout 10s;
		#proxy_timeout 20s; 
    	}
}

測試

數據表要加上ENGINE=ndbcluster才能夠同步過去

鏈接 192.168.2.169:3306

create database mc_test;

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '用戶名',
  `password` varchar(200) NOT NULL COMMENT '密碼',
  PRIMARY KEY (`id`)
) ENGINE=ndbcluster DEFAULT CHARSET=utf8;

INSERT INTO `mc_test`.`account` (`name`, `password`) VALUES ('2', '2');

鏈接 192.168.2.132:3306,192.168.2.133:3306

select * from account;
相關文章
相關標籤/搜索