CentOS安装MariaDB

发布于:2025-02-28 ⋅ 阅读:(11) ⋅ 点赞:(0)

系统环境

操作系统:CentOS 7.9

已安装旧版MariaDB:mysql  Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1

计划安装MariaDB 10.11版本。

步骤

一.如有需要,备份数据库数据。

二.卸载旧版MariaDB。

1.停止MariaDB服务。

sudo systemctl stop mariadb

sudo systemctl disable mariadb

2.卸载MariaDB。

执行卸载:

sudo yum remove mariadb-server mariadb-client mariadb-common

查看是否卸载干净:

rpm -qa | grep -i mariadb

显示还有两个残留:

mariadb-5.5.68-1.el7.x86_64

mariadb-libs-5.5.68-1.el7.x86_64

继续卸载:

sudo yum remove mariadb-5.5.68-1.el7.x86_64 mariadb-libs-5.5.68-1.el7.x86_64

再次查询是否卸载干净:

rpm -qa | grep -i mariadb

确认卸载干净,移除旧版MariaDB数据文件和配置文件(请根据实际情况调整):

sudo rm -rf /var/lib/mysql/

sudo rm -rf /etc/my.cnf*

sudo rm -rf /etc/mysql/

三.安装新版MariaDB。

1.创建MariaDB仓库文件。

CentOS系统软件源默认仅包含古老的MariaDB,安装新版本的MariaDB需要创加纳MariaDB仓库。

sudo vi /etc/yum.repos.d/MariaDB.repo

添加以下内容(具体软件版本和系统版本路径可直接浏览Index of /yum/,根据实际情况调整):

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.11.11/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

清理 YUM 缓存:

sudo yum clean all

更新 YUM 缓存:

sudo yum makecache

2.安装MariaDB服务器和客户端。

sudo yum install MariaDB-server MariaDB-client

检查MariaDB版本:

mysql –version

mysql  Ver 15.1 Distrib 10.11.9-MariaDB, for Linux (x86_64) using readline 5.1

3.修改数据库配置。

vi /etc/my.cnf

将配置修改如下:

[mysqld]

# 数据存储目录

# datadir = /mnt/data/mysqldata

datadir = /home/mysqldata



# 套接字文件路径

socket = /var/lib/mariadb/mariadb.sock



# 监听地址和端口

bind-address = 0.0.0.0

port = 3316



# 字符集

character-set-server = utf8mb4

collation-server = utf8mb4_unicode_ci



# 日志文件

log_error = /var/log/mariadb/mariadb.log

slow_query_log = 1

slow_query_log_file = /var/log/mariadb/slow_query.log

long_query_time = 2



# 二进制日志

log_bin = /var/log/mariadb-bin/mariadb-bin.log

expire_logs_days = 7

max_binlog_size = 100M



# 临时表大小

tmp_table_size = 64M

max_heap_table_size = 64M



# 连接设置

max_connections = 5000

max_user_connections = 1000

wait_timeout = 600

interactive_timeout = 600



# 查询缓存

query_cache_type = 0

query_cache_size = 0



# 表定义缓存

table_open_cache = 4000

table_definition_cache = 2000



# InnoDB 配置

innodb_buffer_pool_size = 4G

innodb_buffer_pool_instances = 8

innodb_log_file_size = 256M

innodb_log_buffer_size = 16M

innodb_flush_log_at_trx_commit = 1

innodb_file_per_table = 1

innodb_flush_method = O_DIRECT

innodb_thread_concurrency = 8



# MyISAM 配置

key_buffer_size = 64M



# 其他配置

skip_name_resolve = 1

back_log = 150

thread_cache_size = 100

open_files_limit = 65535



symbolic-links = 0

local-infile = 0

old_passwords = 0



[client]

default-character-set = utf8mb4

connect_timeout = 30

由于自定义了数据文件存放位置,需要手动创建数据库的数据文件夹,并授权。

mkdir /home/mysqldata

chown -R mysql:mysql /home/mysqldata

由于自定义了数据库日志文件存放位置,需要手动创建数据库日志文件夹,并授权。

mkdir /var/log/mariadb /var/log/mariadb-bin

chown -R mysql:mysql mariadb

chown -R mysql:mysql mariadb-bin

由于自定义了socks文件存放位置,需要手动创建socks文件夹。

mkdir /var/lib/mariadb

4.启动数据库。

sudo systemctl start mariadb

会发现无法启动,查看错误:

sudo systemctl status mariadb

有报错:

[Warning] Can't create test file '/home/mysqldata/VM-32-51-centos.lower-test' (Errcode: 2 "No such file or directory")

看报错推测应该是MariaDB没有权限在数据文件夹下创建文件,但是我们前面已经对数据文件夹做了授权。

这里就涉及到新版MariaDB的一个脑残设定,默认不允许在用户目录(/root,/home)下存放数据文件,旧版MariaDB无此问题。

修改mariadb.service配置,关闭Home保护:

vi /usr/lib/systemd/system/mariadb.service

# Prevent accessing /home, /root and /run/user

ProtectHome=true

把true改为false

重新加载服务配置并重启服务:

sudo systemctl daemon-reload

systemctl start mariadb

发现systemctl start mariadb命令卡住不动。

继续查看报错:

sudo systemctl status mariadb

Could not open mysql.plugin table: "Table 'mysql.plugin' doesn't exist". Some plugins may be not loaded

可以发现错误提示有一些数据表找不到。

这里就涉及到新版MariaDB的又一个脑残设定,当数据文件夹为空时,不会自动创建默认数据库和数据表,旧版MariaDB无此问题。

手动将安装MariaDB时默认数据库文件夹下面的文件拷贝至自定义的数据库文件夹:

cp -R /var/lib/mysql/* /home/mysqldata

chown -R mysql:mysql /home/mysqldata

或者重新执行mysql_install_db来装载默认数据库。

/usr/bin/mysql_install_db

再次启动服务:

systemctl start mariadb

终于可以启动服务。

5.配置数据库账户。

以socks方式连接数据库:

mysql -u root

发现无法连接。

以TCP方式连接数据库:

mysql -u root -P9916

发现仍然无法连接。

这里就涉及到新版MariaDB的又一个脑残设定,安装MariaDB后,需要执行一次安全初始化才能正常使用,如果安装后未运行初始化操作,可能会导致权限配置不完整。

运行安全脚本

mysql_secure_installation

提示命令找不到,/usr/bin目录下无此文件,全局搜索:

sudo find / -name mysql_secure_installation

文件并不存在,莫非文件改名了,试试mariadb_secure_installation,文件依然不存在。

这里就涉及到新版MariaDB的又一个脑残设定,mysql_secure_installation被改名为mariadb-secure-installation,不但修改了mysql字样,下划线也被修改为中划线。

执行之:

mariadb-secure-installation

In order to log into MariaDB to secure it, we'll need the current

password for the root user. If you've just installed MariaDB, and

haven't set the root password yet, you should just press enter here.

报错:

ERROR 2002 (HY000): Can't connect to local server through socket '/var/lib/mysql/mysql.sock' (2)

看报错可知,mariadb-secure-installation找不到sock文件,这里显示的sock文件是默认的sock文件路径,而非我们自定义的sock文件路径。

这就涉及到了mariadb-secure-installation的脑残设定,其sock路径是写死的,说明我们必须在修改MariaDB配置(修改sock路径)之前,就执行这个工具。鉴于我们已经提前修改了MariaDB配置,现在只能回头重新修改配置文件,把sock路径指向默认路径。

vi /etc/my.cnf

socket = /var/lib/mariadb/mariadb.sock

socket = /var/lib/mysql/mysql.sock

重新启动服务:

systemctl restart mariadb

重新执行初始化工具:

mariadb-secure-installation

Enter current password for root (enter for none):

OK, successfully used password, moving on...



Setting the root password or using the unix_socket ensures that nobody

can log into the MariaDB root user without the proper authorisation.



You already have your root account protected, so you can safely answer 'n'.



Switch to unix_socket authentication [Y/n] n

 ... skipping.



You already have your root account protected, so you can safely answer 'n'.



Change the root password? [Y/n] Y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!



By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.



Remove anonymous users? [Y/n] Y

 ... Success!



Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.



Disallow root login remotely? [Y/n] n

 ... skipping.



By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.



Remove test database and access to it? [Y/n] n

 ... skipping.



Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.



Reload privilege tables now? [Y/n] Y

 ... Success!



Cleaning up...



All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.



Thanks for using MariaDB!

执行完毕,使用sock方式连接数据库:

mysql -u root

连接成功,接下来可以配置数据库用户。

四.安装数据库备份工具。

Percona XtraBackup 是一个广泛使用的开源工具,专为 MySQL 和 MariaDB 数据库的物理备份而设计。然而,Percona XtraBackup 的某些版本可能与 MariaDB 的最新版本不完全兼容。为了更好地支持 MariaDB,MariaDB 社区开发了一个 XtraBackup 的分支,称为 MariaDB Backup,或者mariabackup。

安装之:

sudo yum install MariaDB-backup