本文主要用来学习MySQL NDB Cluster
解决学习过程中的痛点:需要开启N台VMware虚拟机,电脑不堪重负
使用docker部署,完美解决
本文使用的docker image: mysql/mysql-cluster:8.0
创建mysql_cluster目录,后续操作都在这个目录下
# docker exec -it ndb_management ndb_mgm
# docker exec -it ndb_mysqld1 mysql -uroot -p
# docker exec -it ndb_mysqld2 mysql -uroot -p
version: '3'
networks:
my_cluster:
driver: bridge
services:
# 服务名,在同一个网络下的docker容器之间可以通过服务名访问,
# 所以我在创建容器以及后面的配置文件中没有指定IP
ndb_management:
image: mysql/mysql-cluster:8.0
# 容器名,docker exec等命令用的
container_name: ndb_management
# 容器启动命令
# command会传给entrypoint.sh并启动不同的服务
# 这个就只是启动的 ~ $ ndb_mgmd --ndb-nodeid=1 ...
command: [
'ndb_mgmd',
'--ndb-nodeid=1',
'--reload',
'--initial'
]
# 容器卷,用来配置集群
# 集群配置文件为config.cnf
# 对应到容器里面的/etc/mysql-cluster.cnf,至于为什么,大家可以去看下镜像的源码
volumes:
- ./config.cnf:/etc/mysql-cluster.cnf
- ./my.cnf:/etc/my.cnf
- ./ndb_mgmd:/var/lib/mysql
ports:
- "1186:1186"
# 指定网络
networks:
- my_cluster
ndb_data1:
image: mysql/mysql-cluster:8.0
container_name: ndb_data1
command: [
'ndbd'
]
volumes:
- ./config.cnf:/etc/mysql-cluster.cnf
- ./my.cnf:/etc/my.cnf
- ./ndbd1:/var/lib/mysql
networks:
- my_cluster
ndb_data2:
image: mysql/mysql-cluster:8.0
container_name: ndb_data2
command: [
'ndbd'
]
volumes:
- ./config.cnf:/etc/mysql-cluster.cnf
- ./my.cnf:/etc/my.cnf
- ./ndbd2:/var/lib/mysql
networks:
- my_cluster
ndb_mysqld1:
image: mysql/mysql-cluster:8.0
container_name: ndb_mysqld1
command: [
'mysqld',
'--default-authentication-plugin=mysql_native_password'
]
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: mycluster
volumes:
- ./config.cnf:/etc/mysql-cluster.cnf
- ./my.cnf:/etc/my.cnf
- ./mysql1:/var/lib/mysql
networks:
- my_cluster
ports:
- "3306:3306"
depends_on:
- ndb_management
- ndb_data1
- ndb_data2
ndb_mysqld2:
image: mysql/mysql-cluster:8.0
container_name: ndb_mysqld2
command: [
'mysqld',
'--default-authentication-plugin=mysql_native_password'
]
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./config.cnf:/etc/mysql-cluster.cnf
- ./my.cnf:/etc/my.cnf
- ./mysql2:/var/lib/mysql
networks:
- my_cluster
ports:
- "3307:3306"
depends_on:
- ndb_management
- ndb_data1
- ndb_data2
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# 配置mysqld的初始化和启动参数,像这里我指定ndb_mgmd的地址为docker中的服务名
[mysqld]
ndbcluster
ndb-connectstring=ndb_management
user=mysql
[mysql_cluster]
ndb-connectstring=ndb_management
[ndbd]
connect-string=ndb_management
[ndb_mgm]
connect-string=ndb_management
# file "config.ini" - showing minimal setup consisting of 1 data node,
# 1 management server, and 3 MySQL servers.
# The empty default sections are not required, and are shown only for
# the sake of completeness.
# Data nodes must provide a hostname but MySQL Servers are not required
# to do so.
# If you don't know the hostname for your machine, use localhost.
# The DataDir parameter also has a default value, but it is recommended to
# set it explicitly.
# Note: [db], [api], and [mgm] are aliases for [ndbd], [mysqld], and [ndb_mgmd],
# respectively. [db] is deprecated and should not be used in new installations.
# 配置ndb_mgmd启动时的集群相关信息
[ndbd default]
NoOfReplicas= 2
[mysqld default]
[ndb_mgmd default]
[tcp default]
[ndb_mgmd]
NodeId=1
hostname=ndb_management
DataDir=/var/lib/mysql
[ndbd]
hostname=ndb_data1
DataDir=/var/lib/mysql
[ndbd]
hostname=ndb_data2
DataDir=/var/lib/mysql
# MySQL API 节点不用特意指定hostname
[mysqld]
[mysqld]
最后MySQL NDB Cluster的最简单的环境已经搭建起来了,鼓掌