1 前言
通常我们安装MariaDB会使用yum的安装方式,但是如果你想进一步熟悉MariaDB,我们会使用二进制的安装方式(更进一步的是编译安装)。
2 实践部分
2.1 系统环境
2.1.1 环境信息
OS = CentOS 7.3 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 准备软件
2.2.1 systemd方式启动的包下载(推荐)
cd ~ wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.16/bintar-linux-systemd-x86_64/mariadb-10.2.16-linux-systemd-x86_64.tar.gz
注:软件包分很多种,请看清楚下载,由于是CentOS 7.3 x86_64,当前选取x86_64并且支持systemd启动
2.2.2 service方式启动的包下载(可选)
cd ~ wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.16/bintar-linux-x86_64/mariadb-10.2.16-linux-x86_64.tar.gz
注:软件包分很多种,请看清楚下载,由于是CentOS 7.3 x86_64,当前选取x86_64并且
其他版本请参阅如下链接,
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.2.16-linux*-x86_64.tar.gz
2.3.3 部署软件包
mv mariadb-10.2.16-linux*-x86_64 /usr/local/ ln -s /usr/local/mariadb-10.2.16-linux*-x86_64/ /usr/local/mysql
2.4 配置软件包
2.4.1 初始化系统表
cd /usr/local/mysql ./scripts/mysql_install_db --user=mysql
提示如下即成功,
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ... OK [...]
如果有以下提示,
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
请解决依赖关系包,
yum install -y libaio-devel
2.4.2 确认自动创建的配置文件
ls /etc/my.cnf
显示如下,
/etc/my.cnf
2.4.3 配置目录权限
chown -R root /usr/local/mysql chown -R mysql /usr/local/mysql/data/
2.4.4 配置并测试环境变量
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.2.16-MariaDB, for Linux (x86_64) using readline 5.1
2.4.5 创建日志目录
mkdir -p /var/log/mariadb chown mysql /var/log/mariadb
2.5 软件启动调试
2.5.1 尝试启动
mysqld_safe --user=mysql &
2.5.2 根据提示解决启动错误
grep -i error /var/log/mariadb/mariadb.log
可见如下提示信息:
2018-07-02 22:10:58 0 [ERROR] mysqld: Can't create/write to file '/var/run/mariadb/mariadb.pid' (Errcode: 2 "No such file or directory") 2018-07-02 22:10:58 0 [ERROR] Can't start server: can't create PID file: No such file or directory
根据错误提示创建PID目录
mkdir /var/run/mariadb chown mysql /var/run/mariadb
2.5.3 检查启动进程
pgrep -u root mysqld_safe -a; pgrep -u mysql mysqld -a
显示如下:
3561 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql 3640 /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/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.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 systemd启动方式配置(推荐)
2.6.1.1 部署启动脚本
cp /usr/local/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system/
2.6.1.2 重载脚本
systemctl daemon-reload
2.6.1.3 测试控制脚本
systemctl start mariadb.service systemctl stop mariadb.service systemctl restart mariadb.service systemctl status mariadb.service
2.6.1.4 配置服务自动启动
systemctl enable mariadb.service
2.6.2 service启动方式配置(可选)
2.6.2.1 部署启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2.6.2.2 增加脚本启动所需的环境变量
vim /etc/init.d/mysqld
修改如下配置:
basedir=/usr/local/mysql datadir=/var/lib/mysql mysqld_pid_file_path=/var/run/mariadb/mariadb.pid
2.6.2.3 修复PID目录不创建问题
vim /etc/init.d/mysqld
启动函数中找到如下行并在行前增加代码,
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
修改后代码如下,
mysqld_pid_dir=`echo $mysqld_pid_file_path | awk -F'/' '{NF-=1;OFS="/";print}'` 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" "$@" &
2.6.2.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
2.6.2.5 修复提示信息错乱问题
vim /etc/init.d/mysqld
修改如下行,
echo $echo_n "Starting MariaDB"
修改后如下,
echo -e $echo_n "Starting MariaDB\n"
2.6.2.6 测试控制脚本
/etc/init.d/mysqld start /etc/init.d/mysqld stop /etc/init.d/mysqld restart /etc/init.d/mysqld status
2.6.2.7 设置服务自启动
chkconfig mysqld on
2.7 优化配置
2.7.1 定制客户端默认的sock
vim /etc/my.cnf.d/mysql-clients.cnf
加入如下配置:
[mysql] port=3306 socket=/var/lib/mysql/mysql.sock
重启使配置生效,
systemctl restart mariadb.service
或者是,
/etc/init.d/mysqld restart
测试登录,
mysql -uroot -p
直接按下回车登录,显示如下,
Enter password: [...] MariaDB [(none)]>
2.7.2 初始化数据库
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.3 注册库文件
echo /usr/local/mariadb-10.2.16-linux-x86_64/lib/ > /etc/ld.so.conf.d/mariadb-10.2.16-x86_64.conf ldconfig
参阅文档:
==================
MariaDB Binary Tarballs 安装向导
——————————–
https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/
MariaDB 下载
——————————–
https://downloads.mariadb.com/MariaDB/
没有评论