欢迎来到资源库(www.zyku.net)

CentOS

当前位置:首页 > 服务器教程 > CentOS > MariaDB

Centos 7.6 源码编译安装 MariaDB 10.5.9

时间:2022-07-07|栏目:CentOS|点击:|我要投稿

本文主要记录如何在 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' http://docs.mengchu.net/web/image/20220706214938452npqgcxnn.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

CMake 编译报错?

CMake 错误请删除 CMakeCache.txt 并重新执行。否则每次读取这个文件,命令修改正确也是报错

rm -f CMakeCache.txt
  • 编译并安装
make && make install && cd

编译报错?

报错:GCC 5.3 or newer is required (-dumpversion says 4.8.5)

原因:GCC 版本低于安装软件要求的版本

MariaDB 编译 TokuDB 引擎时会用到 C++11 标准,系统里 GCC 最高版本 4.8.5 是支持 C++11 标准的,可就算指定了 -DCMAKE_CXX_FLAGS=-std=c++11 也不行。手动编译更高版本的 GCC 又太麻烦,这里直接安装了 devtoolset-9 ,当然你禁用 TokuDB 引擎也无可厚非。

  • Devtoolset-toolchainGCC 工具链
gcc --version	# 查询 GCC 版本
> gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
> Copyright (C) 2015 Free Software Foundation, Inc.

# 安装 SCL 源
yum install -y centos-release-scl scl-utils-build
yum install -y devtoolset-9

# 切换到 Devtoolset-9
scl enable devtoolset-9 bash

gcc --version	# 版本达到 9 系列
> gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
> Copyright (C) 2019 Free Software Foundation, Inc.

报错:c++: fatal error: Killed signal terminated program cc1plus

原因:内存不足所致,可通过设置 2G 交换分区来解决该问题

sudo dd if=/dev/zero of=/swapfile bs=64M count=32	# 设置交换分区的大小
sudo mkswap /swapfile	# 创建交换分区文件
sudo swapon /swapfile	# 开启交换分区

# 编译完成后,回收这部分临时的交换空间
sudo swapoff /swapfile	# 先关闭交换空间
sudo rm /swapfile	# 删除交换空间

配置 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

作者:白小七羽

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

(资源库 www.zyku.net)

原文链接:https://blog.csdn.net/xiaoqy2014/article/details/116245825

上一篇:CentOS 7 开机/etc/rc.local 不执行的问题

栏    目:CentOS

下一篇:解决Failed to download metadata for repo ‘AppStream’

本文标题:Centos 7.6 源码编译安装 MariaDB 10.5.9

本文地址:https://www.zyku.net/centos/12049.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务