> 文章列表 > MySQL NDB Cluster使用docker compose一键部署

MySQL NDB Cluster使用docker compose一键部署

MySQL NDB Cluster使用docker compose一键部署

本文主要用来学习MySQL NDB Cluster
解决学习过程中的痛点:需要开启N台VMware虚拟机,电脑不堪重负
使用docker部署,完美解决

本文使用的docker image: mysql/mysql-cluster:8.0

创建mysql_cluster目录,后续操作都在这个目录下

创建docker-compose.yaml

# 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:ndb_management:image: mysql/mysql-cluster:8.0container_name: ndb_managementcommand: ['ndb_mgmd','--ndb-nodeid=1','--reload','--initial']volumes:- ./config.cnf:/etc/mysql-cluster.cnf- ./my.cnf:/etc/my.cnf- ./ndb_mgmd:/var/lib/mysqlports:- "1186:1186"networks:- my_clusterndb_data1:image: mysql/mysql-cluster:8.0container_name: ndb_data1command: ['ndbd']volumes:- ./config.cnf:/etc/mysql-cluster.cnf- ./my.cnf:/etc/my.cnf- ./ndbd1:/var/lib/mysqlnetworks:- my_clusterndb_data2:image: mysql/mysql-cluster:8.0container_name: ndb_data2command: ['ndbd']volumes:- ./config.cnf:/etc/mysql-cluster.cnf- ./my.cnf:/etc/my.cnf- ./ndbd2:/var/lib/mysqlnetworks:- my_clusterndb_mysqld1:image: mysql/mysql-cluster:8.0container_name: ndb_mysqld1command: ['mysqld','--default-authentication-plugin=mysql_native_password']environment:MYSQL_ROOT_PASSWORD: 123456MYSQL_DATABASE: myclustervolumes:- ./config.cnf:/etc/mysql-cluster.cnf- ./my.cnf:/etc/my.cnf- ./mysql1:/var/lib/mysqlnetworks:- my_clusterports:- "3306:3306"depends_on:- ndb_management- ndb_data1- ndb_data2ndb_mysqld2:image: mysql/mysql-cluster:8.0container_name: ndb_mysqld2command: ['mysqld','--default-authentication-plugin=mysql_native_password']environment:MYSQL_ROOT_PASSWORD: 123456volumes:- ./config.cnf:/etc/mysql-cluster.cnf- ./my.cnf:/etc/my.cnf- ./mysql2:/var/lib/mysqlnetworks:- my_clusterports:- "3307:3306"depends_on:- ndb_management- ndb_data1- ndb_data2

创建my.cnf

# 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]
ndbcluster
ndb-connectstring=ndb_management
user=mysql[mysql_cluster]
ndb-connectstring=ndb_management[ndbd]
connect-string=ndb_management[ndb_mgm]
connect-string=ndb_management

创建config.cnf

# 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.[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[mysqld]
[mysqld]

完成相关卷目录创建

启动 docker-compose up -d
启动成功

docker-compose前3行注释写了怎么进入数据库和ndb_mgm

最后MySQL NDB Cluster的最简单的环境已经搭建起来了,鼓掌👏