Centos源码安装MariaDB 基于GTID主从部署(一遍过)
MariaDB安装
安装依赖
yum install cmake ncurses ncurses-devel bison
下载源码
// 下载源码
wget https://downloads.mariadb.org/interstitial/mariadb-10.6.20/source/mariadb-10.6.20.tar.gz
// 解压源码
tar xzvf mariadb-10.5.9.tar.gz
编译安装
cmake -DCMAKE_INSTALL_PREFIX=/home/mariadb10/node5308/mysql \
-DDEFAULT_CHARSET=utf8 \
-DWITH_DEBUG=1 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/all_boost ..
make -j`nproc`;make install
说明:在编译过程中可能会遇到如下错误:
解决方案:下载对应文件,并将其复制到对应目录下即可!
配置文件
配置文件如下:
[client]
user=mariadb10
socket=/home/mariadb10/node5307/tmp/mysql.sock
[mysqld]
user=mariadb10
core-file
default-storage-engine = InnoDB
basedir = /home/mariadb10/node5307/mysql
datadir = /home/mariadb10/node5307/data
tmpdir = /home/mariadb10/node5307/tmp
#plugin-dir = /home/mariadb10/node5307/mysql/lib/plugin
pid-file = /home/mariadb10/node5307/tmp/mysql.pid
port = 5307
socket = /home/mariadb10/node5307/tmp/mysql.sock
log-error=/home/mariadb10/node5307/logs/mysqld.log
# mysql服务ID,保证整个集群环境中唯一,默认为1
server-id=20241231
# 是否只读,1代表只读,0代表读写
read-only=0
init-file=/home/mariadb10/init.file
# MySQL最大连接数
max_connections = 5510
# InnoDB Buff Pool
innodb_buffer_pool_size = 4G
# 日志文件大小
innodb_log_file_size = 1G
innodb_flush_log_at_trx_commit = 1
#innodb_buffer_pool_instances = 8
innodb_io_capacity_max = 20000
innodb_io_capacity = 20000
thread_cache_size = 16
初始化文件
初始化文件(init.file)内容如下:
# 下面所有的操作都不写入binlog
set session sql_log_bin = 0;
delete from mysql.user where user="s01";
# 设置root用户密码
alter user 'root'@'localhost' identified by 'kingsoft.';
# 创建一个新的用户
create user 's01'@'%' identified by 'kingsoft.';
# 把用户host权限改为'%'
update mysql.user set host='%' where user='root';
update mysql.user set host='%' where user='s01';
FLUSH PRIVILEGES;
# 对上述用户进行授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
GRANT ALL PRIVILEGES ON *.* TO 's01'@'%';
GRANT XA_RECOVER_ADMIN ON *.* TO root@'%';
FLUSH PRIVILEGES;
初始化脚本
初始化脚本(initial.sh)如下:
#!/bin/bash
./clean.sh
/home/mariadb10/node5307/mysql/scripts/mariadb-install-db --defaults-file=/home/mariadb10/node5307/my.cnf
启动脚本
启动脚本(start.sh)如下:
#!/bin/bash
nohup /home/mariadb10/node5307/mysql/bin/mysqld --defaults-file=/home/mariadb10/node5307/my.cnf > /home/mariadb10/node5307/start.log 2>&1 &
mariadb tree
[root@vm172-0-11-157 mariadb10]# tree -L 2
.
├── init.file
├── node5307
│ ├── clean.sh
│ ├── data
│ ├── initial.sh
│ ├── logs
│ ├── my.cnf
│ ├── mysql
│ ├── nohup.out
│ ├── start.log
│ ├── start.sh
│ ├── stop.sh
│ └── tmp
└──
根据自己需要安装,我在本地装了三个实例!
初始化 & 启动 & 连接
// 初始化
./initial.sh
// 启动
./start.sh
/home/mariadb10/node5307/mysql/bin/mysql --defaults-file=/home/mariadb10/node5307/my.cnf -uroot -h127.0.0.1 -pkingsoft. -P5307
连接结果:
MariaDB主从部署
MySQL复制有两种方式:基于日志点的复制、基于GTID的复制,两种方式都依赖于MySQL二进制日志。
# 二进制日志格式
binlog-format=STATEMENT|ROW|MIXED
# ROW格式下日志的级别
binlog-row-image=full|minimal|noblob
基于GTID的复制
基于GTID的复制是MySQL5.6后的一种新的复制方式。MariaDB从10.0.2开始默认开启GTID。
GTID即全局事务ID,其保证了每个在Master上提交的事务在复制集群中可以生成一个唯一的ID。
GTID由3段组成,domain ID-server ID-sequence number。
-
domain ID:单master的环境下, domain id默认值0;多源复制是,domain id区分多个源;
-
server ID:各master节点的sever-id,下面的配置文件有此项;
-
sequence number:事务序列号。
基于GTID的主从部署详细步骤如下:
创建复制用户
在Master上创建复制用户:
MariaDB [(none)]> create user 'repl'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)
FLUSH PRIVILEGES;
Master参数配置
[mysqld]
log-bin=mysql-bin
relay-log=mysql-relay-bin
# 此两项为打开从服务器崩溃二进制日志功能,信息记录在事物表而不是保存在文件
master-info-repository=TABLE
relay-log-info-repository=TABLE
Slave参数配置
# ************从服务器*************
log-bin=mysql-bin
relay-log=mysql-relay-bin
master_info_repository = TABLE
relay_log_info_repository = TABLE
初始化数据(可选)
备份Master上的数据,还原到Slave:
# 使用mysqldump备份数据
mysqldump --master-data=2 --single-transaction --routines --all-databases -uroot -p >> dump.sql;
# 或使用xtrabackup
xtrabackup --slave-info ...
# 还原
mysql -uroot -p < all.sql
注:复制用户也会被还原到Slave!
Slave指向Master
// 用户配置不好可能导致用户权限不足
change master to master_host = '127.0.0.1',master_port=5307,master_user = 'repl',master_password = '123456',master_use_gtid = slave_pos;
// 也可以直接使用默认的root用户
change master to master_host = '127.0.0.1',master_port=5307,master_user = 'root',master_password = '123456',master_use_gtid = slave_pos;
# 启动主从复制
start slave;
若在配置过程中遇到新建用户无法连接问题,请参考:数据库新建用户后(Host:%),报错:localhost无法连接-CSDN博客
检查Slave状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: root
Master_Port: 5307
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 342
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 641
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 342
Relay_Log_Space: 1757
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 20241231
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Slave_Pos
Gtid_IO_Pos: 0-20241231-3
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 1
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
GTID相关命令
# 查看master当前GTID值
select @@global.gtid_current_pos;
# 查看slave当前GTID值
select @@global.gtid_slave_pos;
# 从库采用gtid的复制
change master to master_use_gtid = { slave_pos | current_pos | no }