如何在RHEL8部署MariaDB二进制包?

MySQL & MariaDB

1 前言

基于RHEL 8的系统,上一篇MariaDB二进制部署已经不再适用,本章将针对新的环境进行设定。

2 实践部分

2.1 系统环境

2.1.1 环境信息

OS = RHEL 8.0 x86_64
IP Address = 10.168.0.70
hostname = mariadb_bin.cmdschool.org

2.1.2 防火墙配置(可选)

firewall-cmd --permanent --add-service mysql
firewall-cmd --reload
firewall-cmd --list-all

2.2 准备软件

cd ~
wget https://downloads.mariadb.com/MariaDB/mariadb-10.3.15/bintar-linux-x86_64/mariadb-10.3.15-linux-x86_64.tar.gz

其他版本请参阅如下链接,
https://downloads.mariadb.com/MariaDB/
如果想知道当前最新的稳定版本,请参阅如下页面,
https://downloads.mariadb.org/mariadb/

2.3 安装软件包

2.3.1 创建运行用户

groupadd  -g 27 mysql
useradd -u 27 -g 27 -d /var/lib/mysql -c 'MySQL Server' -s /bin/false mysql

2.3.2 解压软件包

cd ~
tar -xf mariadb-10.3.15-linux-x86_64.tar.gz

2.3.3 部署软件包

mv mariadb-10.3.15-linux-x86_64 /usr/local/
ln -s /usr/local/mariadb-10.3.15-linux-x86_64/ /usr/local/mysql

2.4 配置软件包

2.4.1 配置目录权限

chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data/

2.4.2 配置并测试环境变量

echo 'export MYSQL_HOME=/usr/local/mysql' > /etc/profile.d/mysql.sh
echo 'export PATH=${MYSQL_HOME}/bin:$PATH' >> /etc/profile.d/mysql.sh
echo 'export PATH=${MYSQL_HOME}/scripts:$PATH' >> /etc/profile.d/mysql.sh
source /etc/profile
mysql -V

正常情况下你会看到如下提示,

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

如果遇到以下错误提示,

mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

你需要安装如下包解决依赖关系,

yum install -y ncurses-compat-libs

2.4.3 创建以下配置文件

vim /etc/my.cnf

加入如下配置,

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections=1000

[mysqld_safe]
log-error=/var/log/mysqld/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
port=3306
socket=/var/lib/mysql/mysql.sock

!includedir /etc/my.cnf.d

另外,使用以下命令确保子配置文件目录被创建,

mkdir -p /etc/my.cnf.d

2.4.4 初始化系统表

cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql

提示如下即成功,

Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK
[...]

2.4.5 创建日志目录

mkdir -p /var/log/mysqld
chown mysql /var/log/mysqld

2.5 软件启动调试

2.5.1 尝试启动

mysqld_safe --user=mysql &

你会看到如下提示信息,

190617 02:27:56 mysqld_safe Logging to '/var/log/mysqld/mysqld.log'.
190617 02:27:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

我们建议你通过如下命令检查输出是否包含错误日志,

grep -i error /var/log/mysqld/mysqld.log

如果发现有如下错误日志信息,

2019-06-17  2:27:56 0 [ERROR] mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2 "No such file or directory")
2019-06-17  2:27:56 0 [ERROR] Can't start server: can't create PID file: No such file or directory

则需要根据错误提示创建PID目录后再试,

mkdir /var/run/mysqld/
chown mysql /var/run/mysqld/

2.5.2 检查启动进程

pgrep -u root mysqld_safe -a; pgrep -u mysql mysqld -a

显示如下:

3724 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
3818 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock

2.5.4 检查倾听的端口

netstat -antp | grep 3306

显示如下:

tcp6       0      0 :::3306                 :::*                    LISTEN      3640/mysqld

2.5.6 手动停止服务

kill 2 `pgrep -u mysql mysqld`

2.6 部署启动控制

2.6.1 关闭SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

2.6.2 部署启动脚本

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

2.6.3 增加脚本启动所需的环境变量

vim /etc/init.d/mysqld

修改如下配置:

basedir=/usr/local/mysql
datadir=/var/lib/mysql
mysqld_pid_file_path=/var/run/mysqld/mysqld.pid

另外,你也可以使用以下快速命令配置,

sed -i 's/^basedir=/basedir=\/usr\/local\/mysql/g' /etc/init.d/mysqld
sed -i 's/^datadir=/datadir=\/var\/lib\/mysql/g' /etc/init.d/mysqld
sed -i 's/^mysqld_pid_file_path=/mysqld_pid_file_path=\/var\/run\/mysqld\/mysqld.pid/g' /etc/init.d/mysqld

2.6.4 修复启动检测函数异常问题

vim /etc/init.d/mysqld

修改如下行,

if $bindir/mysqladmin ping >/dev/null 2>&1; then

修改后如下,

if $bindir/mysqladmin --protocol=tcp ping >/dev/null 2>&1; then

另外,你也可以使用以下快速命令配置,

sed -i 's/if $bindir\/mysqladmin ping/if $bindir\/mysqladmin --protocol=tcp ping/g' /etc/init.d/mysqld

2.6.5 修复PID目录不创建问题

vim /etc/init.d/mysqld

启动函数中找到如下行并在行前增加代码,

      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &

修改后代码如下,

      mysqld_pid_dir=`dirname $mysqld_pid_file_path`
      if ! test -d $mysqld_pid_dir
      then
        mkdir $mysqld_pid_dir
        chown mysql $mysqld_pid_dir
      fi
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &

另外,你也可以使用以下快速命令配置,

sed -i 's/$bindir\/mysqld_safe --datadir=/\n      mysqld_pid_dir=`dirname $mysqld_pid_file_path`\n      if ! test -d $mysqld_pid_dir\n      then\n        mkdir $mysqld_pid_dir\n        chown mysql $mysqld_pid_dir\n      fi\n      $bindir\/mysqld_safe --datadir=/g' /etc/init.d/mysqld

2.6.6 测试控制脚本

/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
/etc/init.d/mysqld status

2.6.7 设置服务自启动

chkconfig mysqld on

2.7 优化MariaDB

2.7.1 初始化数据库

mysql_secure_installation --defaults-file=/etc/my.cnf

向导如下:

[...]
Set root password? [Y/n] y
New password:
Re-enter new password:
[...]
Remove anonymous users? [Y/n] y
[...]
Disallow root login remotely? [Y/n] n
[...]
Remove test database and access to it? [Y/n] y
[...]
Reload privilege tables now? [Y/n] y
[...]

2.7.2 登录并测试配置

mysql -uroot -p

2.7.3 注册库文件

echo /usr/local/mariadb-10.3.15-linux-x86_64/lib/ > /etc/ld.so.conf.d/mariadb-10.3.15-x86_64.conf
ldconfig

参阅文档:
==================

MariaDB Binary Tarballs 安装向导
——————————–
https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/

MariaDB 下载
——————————–
https://downloads.mariadb.com/MariaDB/

没有评论

发表评论

MySQL & MariaDB
如何开启Mariadb或MySQL Binlog?

1 前言 一个问题,一篇文章,一出故事。 笔者需要备份Mariadb的binlog,于是整理此文。 …

MySQL & MariaDB
如何迁移MariaDB或MySQL数据目录?

1 前言 一个问题,一篇文章,一出故事。 笔者之前的数据库使用默认部署在根目录下,现在想迁移至专用的 …

MySQL & MariaDB
如何基于CentOS 8.x安装MariaDB?

1 基础知识 – MariaDB数据库管理系统是MySQL的一个分支 – M …