ubuntu 搭建mysql cluster

开始前介绍

docker ubuntu 版本:latest (20.04)
mysql-cluster 版本 :mysql-cluster-8.0.28-linux-glibc2.12-x86_64.tar.gz
ubuntu 地址172.17.0.2(msg);172.17.0.3,172.17.0.4(node)
本次集群是通过docker 模拟搭建出来的。这里默认系统已经搭建好了docker
主要节点有cluster-msg,cluster-node-1,cluster-node2
三个服务。
将mysql-cluster 上传到服务器里路径:/usr/local/

开始前准备

准备 config.ini (msg 管理需要)

mkdir -p /usr/local/mysql-cluster/msg
vim /usr/local/mysql-cluster/msg/config.ini
#将一下配置拷进去
[NDBD DEFAULT]
NoOfReplicas =2
DataMemory = 512M
IndexMemory = 64M
[NDB_MGMD]
NodeId=1
hostname =172.17.0.2
datadir =/var/lib/mysql-cluster
[NDBD]
NodeId =2
hostname =172.17.0.4
datadir =/usr/local/mysql-cluster/data
[NDBD]
NodeId =3
hostname =172.17.0.3
datadir =/usr/local/mysql-cluster/data
[mysqld]
NodeId =4
hostname =172.17.0.4
[mysqld]
NodeId =5
hostname =172.17.0.3

准备配置my.cnf (node 节点需要)

mkdir -p /usr/local/mysql-cluster/node
vim /usr/local/mysql-cluster/node/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/sock/mysql.sock
symbolic-links=0
ndbcluster
ndb-connectstring=172.17.0.2
[mysql_cluster]
ndb-connectstring=172.17.0.2
[mysqld_safe]
log-error=/usr/local/mysql/logs/mysql.pid
pid-file=/usr/local/mysql/logs/error.log
[mysql]
socket=/usr/local/mysql/sock/mysql.sock
开始配置msg (172.17.0.2)
#解压缩
tar vxf mysql-cluster-8.0.28-linux-glibc2.12-x86_64.tar.gz
#重命名
mv mysql-cluster-8.0.28-linux-glibc2.12-x86_64
mysql-cluster
#启动docker
docker run -it -d -v /usr/local/mysql-cluster/msg:/usr/local/mysql-cluster -v /usr/local/mysql:/usr/local/mysql --name mysql-cluster-msg ubuntu
#进入 msg容器
docker exec -it mysql-cluster-msg /bin/bash
#确认当前容器得ip 是172.17.0.2
root@cdcb6a1a12bf:/usr/local/mysql-cluster# hostname -i  
172.17.0.2
cd /usr/local/mysql/bin
#确认 ndb_mgmd 文件在这里
root@cdcb6a1a12bf:/usr/local/mysql/bin# ls
ibd2sdi                    mysqladmin      ndb_desc               ndb_restore
innochecksum               mysqlbinlog     ndb_drop_index         ndb_select_all
lz4_decompress             mysqlcheck      ndb_drop_table         ndb_select_count
my_print_defaults          mysqld          ndb_error_reporter     ndb_show_tables
myisam_ftdump              mysqld-debug    ndb_import             ndb_size.pl
myisamchk                  mysqld_multi    ndb_index_stat         ndb_top
myisamlog                  mysqld_safe     ndb_mgm                ndb_waiter
myisampack                 mysqldump       ndb_mgmd               ndbd
mysql                      mysqldumpslow   ndb_move_data          ndbinfo_select_all
mysql_config               mysqlimport     ndb_perror             ndbmtd
mysql_config_editor        mysqlpump       ndb_print_backup_file  ndbxfrm
mysql_migrate_keyring      mysqlshow       ndb_print_file         perror
mysql_secure_installation  mysqlslap       ndb_print_frag_file    zlib_decompress
mysql_ssl_rsa_setup        ndb_blob_tool   ndb_print_schema_file
mysql_tzinfo_to_sql        ndb_config      ndb_print_sys_file
mysql_upgrade              ndb_delete_all  ndb_redo_log_reader
#开始初始化
./ndb_mgmd -f /usr/local/mysql-cluster/config.ini --initial
#结果现实
root@cdcb6a1a12bf:/usr/local/mysql/bin# ./ndb_mgmd -f /usr/local/mysql-cluster/config.ini --initial
./ndb_mgmd: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
#执行如下命令
apt update && apt install -y libtinfo5 --allow-remove-essential
#再次执行命令
ntialcdcb6a1a12bf:/usr/local/mysql/bin# ./ndb_mgmd -f /usr/local/mysql-cluster/config.ini --initial
MySQL Cluster Management Server mysql-8.0.28 ndb-8.0.28
2023-01-04 07:12:22 [MgmtSrvr] INFO     -- The default config directory '/usr/local/mysql/mysql-cluster' does not exist. Trying to create it...
2023-01-04 07:12:22 [MgmtSrvr] INFO     -- Sucessfully created config directory
2023-01-04 07:12:22 [MgmtSrvr] WARNING  -- at line 4: [DB] IndexMemory is deprecated, will use Number bytes on each ndbd(DB) node allocated for storing indexes instead
2023-01-04 07:12:22 [MgmtSrvr] WARNING  -- at line 4: [DB] IndexMemory is deprecated, will use Number bytes on each ndbd(DB) node allocated for storing indexes instead - Repeated 2 times
2023-01-04 07:12:22 [MgmtSrvr] ERROR    -- Directory '/var/lib/mysql-cluster' specified with DataDir in configuration does not exist.
#失败 缺少/var/lib/mysql-cluster 执行命令
mkdir -p /var/lib/mysql-cluster
重新执行命令
./ndb_mgmd -f /usr/local/mysql-cluster/config.ini --initial
MySQL Cluster Management Server mysql-8.0.28 ndb-8.0.28
2023-01-04 07:22:48 [MgmtSrvr] WARNING  -- at line 4: [DB] IndexMemory is deprecated, will use Number bytes on each ndbd(DB) node allocated for storing indexes instead
#执行成功
#执行命令 
root@cdcb6a1a12bf:/usr/local/mysql/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 172.17.0.4)
id=3 (not connected, accepting connect from 172.17.0.3)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @172.17.0.2  (mysql-8.0.28 ndb-8.0.28)

[mysqld(API)]   2 node(s)
id=4 (not connected, accepting connect from 172.17.0.4)
id=5 (not connected, accepting connect from 172.17.0.3)

ndb_mgm> 
mgm 执行完成
开始搭建node-1
docker run -it -d -v /usr/local/mysql-cluster/node:/usr/local/mysql-cluster -v /usr/local/mysql:/usr/local/mysql -p 3307:3306 --name mysql-cluster-node-1 ubuntu
root@hecs-337478:/usr/local/mysql-cluster/node# docker exec -it mysql-cluster-node-1 /bin/bash
root@c75ec054d646:/# hostname -i
172.17.0.3
#将my.cnf 拷贝到 /etc/my.cnf
cp /usr/local/mysql-cluster/my.cnf /etc/my.cnf
#进入执行文件
cd /usr/local/mysql/bin
root@c75ec054d646:/usr/local/mysql/bin# ./ndbd --initial   
2023-01-04 07:34:52 [ndbd] INFO     -- Angel connected to '172.17.0.2:1186'
2023-01-04 07:34:52 [ndbd] INFO     -- Angel allocated nodeid: 3
2023-01-04 07:34:52 [ndbd] WARNING  -- Cannot change directory to '/usr/local/mysql-cluster/data', error: 2
2023-01-04 07:34:52 [ndbd] ERROR    -- Couldn't start as daemon, error: 'Failed to open logfile '/usr/local/mysql-cluster/data/ndb_3_out.log' for write, errno: 2
#执行失败 文件
#执行如下命令
mkdir /usr/local/mysql-cluster/data
#重新执行命令
./ndbd --initial
root@c75ec054d646:/usr/local/mysql/bin# ./ndbd --initial
2023-01-04 07:36:05 [ndbd] INFO     -- Angel connected to '172.17.0.2:1186'
2023-01-04 07:36:05 [ndbd] INFO     -- Angel allocated nodeid: 3
#执行成功
开始搭建node-2
docker run -it -d -v /usr/local/mysql-cluster/node:/usr/local/mysql-cluster -v /usr/local/mysql:/usr/local/mysql -p 3306:3306 --name mysql-cluster-node-2 ubuntu
root@hecs-337478:/usr/local/mysql-cluster/node# docker exec -it mysql-cluster-node-1 /bin/bash
root@c75ec054d646:/# hostname -i
172.17.0.3
#将my.cnf 拷贝到 /etc/my.cnf
cp /usr/local/mysql-cluster/my.cnf /etc/my.cnf
#进入执行文件
cd /usr/local/mysql/bin
root@c75ec054d646:/usr/local/mysql/bin# ./ndbd --initial   
2023-01-04 07:34:52 [ndbd] INFO     -- Angel connected to '172.17.0.2:1186'
2023-01-04 07:34:52 [ndbd] INFO     -- Angel allocated nodeid: 3
2023-01-04 07:34:52 [ndbd] WARNING  -- Cannot change directory to '/usr/local/mysql-cluster/data', error: 2
2023-01-04 07:34:52 [ndbd] ERROR    -- Couldn't start as daemon, error: 'Failed to open logfile '/usr/local/mysql-cluster/data/ndb_3_out.log' for write, errno: 2
#执行失败 文件
#执行如下命令
mkdir /usr/local/mysql-cluster/data
#重新执行命令
./ndbd --initial
root@c75ec054d646:/usr/local/mysql/bin# ./ndbd --initial
2023-01-04 07:36:05 [ndbd] INFO     -- Angel connected to '172.17.0.2:1186'
2023-01-04 07:36:05 [ndbd] INFO     -- Angel allocated nodeid: 3
#执行成功

查看mgm 节点连上了

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2 (not connected, accepting connect from 172.17.0.4)
id=3    @172.17.0.3  (mysql-8.0.28 ndb-8.0.28, starting, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @172.17.0.2  (mysql-8.0.28 ndb-8.0.28)

[mysqld(API)]   2 node(s)
id=4 (not connected, accepting connect from 172.17.0.4)
id=5 (not connected, accepting connect from 172.17.0.3)
#连接所有节点之后 再次执行
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @172.17.0.4  (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0, *)
id=3    @172.17.0.3  (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @172.17.0.2  (mysql-8.0.28 ndb-8.0.28)

[mysqld(API)]   2 node(s)
id=4 (not connected, accepting connect from 172.17.0.4)
id=5    @172.17.0.3  (mysql-8.0.28 ndb-8.0.28)

这里需要注意得是,需要链接所有的节点才能继续安装mysql 否者得话 会导致mysql 链接不上集群

会报如下错误

 0 [ERROR] [MY-010865] [Server] NDB: Tables not available after 120 seconds. Consider increasing --ndb-wait-setup value

重要得事情说三遍 保证所有节点(ndbd)都连接上mgm !保证所有的节点(ndbd)都连接上mgm!保证所有得节点(ndbd)都连接上mgm ! 才可以安装mysql不然会报错的。

开始安装mysql

#执行命令
root@c75ec054d646:/usr/local/mysql/bin# ./mysqld --initialize --user=root
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
root@c75ec054d646:/usr/local/mysql/bin# 
#执行
apt-get update & apt-get install libaio-dev
再次执行
root@c75ec054d646:/usr/local/mysql/bin# ./mysqld --initialize --user=root
./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
#执行
apt-get install numactl
再次执行
root@c75ec054d646:/usr/local/mysql/bin# ./mysqld --initialize --user=root
2023-01-04T07:46:09.014641Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-01-04T07:46:09.019801Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28-cluster) initializing of server in progress as process 570
2023-01-04T07:46:09.078046Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-04T07:46:10.299762Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-04T07:46:12.249271Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: :EY9G=a2Akby
保存好初始密码  EY9G=a2Akby

启动 mysql

root@c75ec054d646:/usr/local/mysql/bin# ./mysqld --user=root
2023-01-04T07:48:05.896979Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-01-04T07:48:05.897101Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28-cluster) starting as process 615
2023-01-04T07:48:05.913412Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-04T07:48:06.087261Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
#这里你以为启动成功了 但是并没有 这里其实启动失败了 因为 我没有启动全部的node 就直接启动mysql过一段时间会报出如下异常
 0 [ERROR] [MY-010865] [Server] NDB: Tables not available after 120 seconds. Consider increasing --ndb-wait-setup value
#如何解决: 启动所有得节点

保证所有得节点都连接上mgm 后再次启动mysql

root@be35e378176d:/usr/local/mysql/bin# ./mysqld --user=root
2023-01-04T08:47:51.512841Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-01-04T08:47:51.512971Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28-cluster) starting as process 424
2023-01-04T08:47:51.526725Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-04T08:47:51.883664Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-04T08:47:53.512744Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-01-04T08:47:53.512787Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-01-04T08:47:53.560897Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock
2023-01-04T08:47:53.560938Z 0 [System] [MY-010931] [Server] /usr/local/mysql/bin/mysqld: ready for connections. Version: '8.0.28-cluster'  socket: '/usr/local/mysql/sock/mysql.sock'  port: 3306  MySQL Cluster Community Server - GPL.
2023-01-04 08:47:54 [NdbApi] INFO     -- Flushing incomplete GCI:s < 251/7
2023-01-04 08:47:54 [NdbApi] INFO     -- Flushing incomplete GCI:s < 251/7

查看msg 链接状态

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @172.17.0.4  (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0, *)
id=3    @172.17.0.3  (mysql-8.0.28 ndb-8.0.28, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @172.17.0.2  (mysql-8.0.28 ndb-8.0.28)

[mysqld(API)]   2 node(s)
id=4    @172.17.0.4  (mysql-8.0.28 ndb-8.0.28)
id=5    @172.17.0.3  (mysql-8.0.28 ndb-8.0.28)

ndb_mgm> 

开始验证

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
update user set host = '%' where user = 'root';

ALTER USER 'root'@'%' IDENTIFIED WITH 'hou123456.' BY '123456';

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352

推荐阅读更多精彩内容