编译安装Mariadb,并启动后可以正常登录

环境: CentOS 7.8,mariadb 10.5.4源码包

  • 创建用户
useradd -r -s /sbin/nologin -d /data/mysql mysql
  • 创建数据目录、安装目录及授权
# 创建数据目录及授权
mkdir -p /data/mysql
chown mysql:mysql /data/mysql

# 创建安装目录及授权
mkdir /usr/local/mysql
chown -R root:mysql /usr/local/mysql/
  • 下载源码包
wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.5.4/source/mariadb-10.5.4.tar.gz
  • 解压
tar xvf mariadb-10.5.4.tar.gz
cd mariadb-10.5.4
  • 编译安装
# 安装软件包
yum install -y bison bison-devel zlib-devel libcurl-devel libarchive-devel \
 boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel \
 openssl-devel libevent-devel libaio-devel

# 编译
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

# 安装
make && make install
  • 准备配置文件
mkdir /etc/mysql/
cp /etc/my.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
# 在[mysqld]中添加(或修改)以下内容
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on
socket=/data/mysql/mysql.sock

[mysql]
socket=/data/mysql/mysql.sock
  • 创建数据库文件
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
  • 准备服务脚本,并启动服务
vim /usr/lib/systemd/system/mariadb.service
# 填写以下内容
[Unit]
Description=MariaDB 10.5.4 database server
After=syslog.target
After=network.target

[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql
TimeoutSec=300
PrivateTmp=false

[Install]
WantedBy=multi-user.target
  • 创建日志文件目录(不创建启动失败)
mkdir /var/log/mariadb
mkdir /var/run/mariadb
chown mysql:mysql /var/log/mariadb
chown mysql:mysql /var/run/mariadb
  • 设置PATH路径并生效
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
  • 启动服务
systemctl start mariadb
  • 登录 mysql
mysql

你可能感兴趣的:(Linux,练习)