開(kāi)始前介紹
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)
本次集群是通過(guò)docker 模擬搭建出來(lái)的蠢古。這里默認(rèn)系統(tǒng)已經(jīng)搭建好了docker
主要節(jié)點(diǎn)有cluster-msg,cluster-node-1,cluster-node2
三個(gè)服務(wù)。
將mysql-cluster 上傳到服務(wù)器里路徑:/usr/local/
開(kāi)始前準(zhǔn)備
準(zhǔn)備 config.ini (msg 管理需要)
mkdir -p /usr/local/mysql-cluster/msg
vim /usr/local/mysql-cluster/msg/config.ini
#將一下配置拷進(jìn)去
[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
準(zhǔn)備配置my.cnf (node 節(jié)點(diǎn)需要)
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
開(kāi)始配置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
#啟動(dòng)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
#進(jìn)入 msg容器
docker exec -it mysql-cluster-msg /bin/bash
#確認(rèn)當(dāng)前容器得ip 是172.17.0.2
root@cdcb6a1a12bf:/usr/local/mysql-cluster# hostname -i
172.17.0.2
cd /usr/local/mysql/bin
#確認(rèn) 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
#開(kāi)始初始化
./ndb_mgmd -f /usr/local/mysql-cluster/config.ini --initial
#結(jié)果現(xiàn)實(shí)
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
#執(zhí)行如下命令
apt update && apt install -y libtinfo5 --allow-remove-essential
#再次執(zhí)行命令
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 執(zhí)行命令
mkdir -p /var/lib/mysql-cluster
重新執(zhí)行命令
./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
#執(zhí)行成功
#執(zhí)行命令
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 執(zhí)行完成
開(kāi)始搭建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
#進(jìn)入執(zhí)行文件
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
#執(zhí)行失敗 文件
#執(zhí)行如下命令
mkdir /usr/local/mysql-cluster/data
#重新執(zhí)行命令
./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
#執(zhí)行成功
開(kāi)始搭建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
#進(jìn)入執(zhí)行文件
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
#執(zhí)行失敗 文件
#執(zhí)行如下命令
mkdir /usr/local/mysql-cluster/data
#重新執(zhí)行命令
./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
#執(zhí)行成功
查看mgm 節(jié)點(diǎn)連上了
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)
#連接所有節(jié)點(diǎn)之后 再次執(zhí)行
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)
這里需要注意得是,需要鏈接所有的節(jié)點(diǎn)才能繼續(xù)安裝mysql 否者得話 會(huì)導(dǎo)致mysql 鏈接不上集群
會(huì)報(bào)如下錯(cuò)誤
0 [ERROR] [MY-010865] [Server] NDB: Tables not available after 120 seconds. Consider increasing --ndb-wait-setup value
重要得事情說(shuō)三遍 保證所有節(jié)點(diǎn)(ndbd)都連接上mgm 津畸!保證所有的節(jié)點(diǎn)(ndbd)都連接上mgm!保證所有得節(jié)點(diǎn)(ndbd)都連接上mgm ! 才可以安裝mysql不然會(huì)報(bào)錯(cuò)的狂秦。
開(kāi)始安裝mysql
#執(zhí)行命令
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#
#執(zhí)行
apt-get update & apt-get install libaio-dev
再次執(zhí)行
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
#執(zhí)行
apt-get install numactl
再次執(zhí)行
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
啟動(dòng) 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.
#這里你以為啟動(dòng)成功了 但是并沒(méi)有 這里其實(shí)啟動(dòng)失敗了 因?yàn)?我沒(méi)有啟動(dòng)全部的node 就直接啟動(dòng)mysql過(guò)一段時(shí)間會(huì)報(bào)出如下異常
0 [ERROR] [MY-010865] [Server] NDB: Tables not available after 120 seconds. Consider increasing --ndb-wait-setup value
#如何解決: 啟動(dòng)所有得節(jié)點(diǎn)
保證所有得節(jié)點(diǎn)都連接上mgm 后再次啟動(dòng)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 鏈接狀態(tài)
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>
開(kāi)始驗(yàn)證
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
update user set host = '%' where user = 'root';
ALTER USER 'root'@'%' IDENTIFIED WITH 'hou123456.' BY '123456';