淘先锋技术网

首页 1 2 3 4 5 6 7

本文主要记录如何在 CentOS 7.6 中编译安装 MariaDB 官方最新的 10.5.9 版本。由于像 NginxMysqlPHP的的源码都是用 C/C++ 写的,所以自己的 CentOS 7.6 服务器上必须要安装 gccg++ 软件。

搭建 LNMP 环境一般是先安装 Mysql/MariaDB ,再安装 Nginx ,其次是安装 PHP

准备工作

创建用户和组

先创建一个名为 mysql 且没有登录权限的系统用户和一个名为 mysql 的系统用户组,然后安装 MariaDB 所需的依赖库和依赖包,最后通过 cmake 进行安装的详细配置。

  • 创建 mysql 系统用户和系统用户组
groupadd -r mysql && useradd -c "MariaDB Server" -r -g mysql -s /sbin/nologin -d /usr/local/mariadb mysql -M

创建数据库相关目录

提前预定 MariaDB 的安装目录为 /usr/local/mariadb 并且数据目录为 /data/mariadb ,赋予 mysql 用户权限。

mkdir -pv /data/mariadb && chown -R mysql:mysql /data/mariadb

删除数据库相关文件

  • 删除 CentOS 默认数据库配置文件
find -H /etc/ | grep my.c
> /etc/my.cnf.d
> /etc/my.cnf.d/mysql-clients.cnf
> /etc/pki/tls/certs/make-dummy-cert
> /etc/pki/tls/certs/renew-dummy-cert
> /etc/my.cnf

rm -rf /etc/my.cnf /etc/my.cnf.d/
  • 卸载系统自带 mariadb-libs
rpm -qa|grep mariadb*
> mariadb-libs-5.5.60-1.el7_5.x86_64

rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps

安装相关包

安装依赖库

  • Yum 安装 GCCGCC-C++C/C++ 语言编译环境
yum -y install gcc gcc-c++ make autoconf automake libtool
  • Yum 安装 MariaDB 必须的依赖库
yum -y install openssl openssl-devel ncurses ncurses-devel bison bison-devel boost boost-devel jemalloc jemalloc-devel bzip2 bzip2-devel libxml2 libxml2-devel perl perl-devel lsof libaio-devel libcurl-devel libarchive-devel libevent-devel pcre-devel pcre2-devel zlib-devel kernel-headers kernel-devel zip tar m4 git gnutls-devel

安装编译包

  • CMake :编译工具

(备用:https://blog.xiaoqy.com/pub/packages/cmake/cmake-3.19.5.tar.gz)

wget -P '/usr/local/src' https://cmake.org/files/v3.19/cmake-3.19.5.tar.gz \
&& cd /usr/local/src \
&& tar -zxvf cmake-3.19.5.tar.gz -C '/usr/local/src' \
&& cd cmake-3.19.5

./bootstrap
gmake && gmake install	# 或者 make && make install

cmake --version

编译安装 MariaDB

  • 下载并解压文件

(备用:https://blog.xiaoqy.com/pub/packages/mariadb/mariadb-10.5.9.tar.gz

wget -P '/usr/local/src' https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.5.9/source/mariadb-10.5.9.tar.gz \
&& cd /usr/local/src \
&& tar -zxvf mariadb-10.5.9.tar.gz -C '/usr/local/src' \
&& cd mariadb-10.5.9
  • 预编译
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
-DMYSQL_UNIX_ADDR=/data/mariadb/mysql.sock \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/data/mariadb \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITHOUT_TOKUDB=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_BOOST=system \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=1
  • 编译并安装

配置 MariaDB

# 使用 mysql 用户执行脚本, 安装数据库到数据库存放目录
/usr/local/mariadb/scripts/mysql_install_db --user=mysql --datadir=/data/mariadb
  • 复制 MariaDB 配置文件到 /etc 目录
# 拷贝 maria 安装目录下 support-files 目录下的文件 wsrep.cnf 到 /etc 目录并重命名为 my.cnf
cp /usr/local/mariadb/support-files/wsrep.cnf /etc/my.cnf
  • 创建启动脚本
cp /usr/local/mariadb/support-files/mysql.server /etc/rc.d/init.d/mysqld
  • 启动 mysqld 服务
/etc/rc.d/init.d/mysqld start
  • 配置环境变量
# 写入环境变量到 mysql.sh
echo -e "export PATH=\$PATH:/usr/local/mariadb/bin/" > /etc/profile.d/mysql.sh

# 为脚本赋于可执行权限
chmod 0777 /etc/profile.d/mysql.sh

# 读取并执行 mysql.sh 脚本, 并执行脚本, 以立即生效环境变量
source /etc/profile.d/mysql.sh
  • 初始化 MariaDB
# 运行 MariaDB 初始化脚本
mysql_secure_installation
 
# 以下提示:
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
    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.
 
    Enter current password for root (enter for none):	# 直接Enter,预设MariaDB没有密码
    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] y	# y,切换到unix_socket身份验证
    Enabled successfully!
    Reloading privilege tables..
     ... Success!
 
 
    You already have your root account protected, so you can safely answer 'n'.

    Change the root password? [Y/n] y	# y,设定root密码
    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	# 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] y	# y,移除远端登入权限
     ... Success!
 
    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] y	# y,移除测试资料库和账号
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
 
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
 
    Reload privilege tables now? [Y/n] y	# 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!
  • 设置 MariaDB 为自启动并启动服务
systemctl enable mysqld.service && systemctl start mysqld.service
  • 查询 MariaDB 状态
systemctl status mysqld.service

# 或者
ps -ef | grep mysqld
netstat -anp | grep mysqld

作者:xiaoqy
原文链接:https://blog.xiaoqy.com/215.html