linux系统安装mariadb

一、下载安装

mariadb是属于mysql的一个分支,是其创始人在mysql被卖给oracle之后重新分出来的,maria取自于他女儿的名字。mariadb完全兼容于mysql,在很多新版本的linux系统中,mysql都已经被替换成了mariadb。

mariadb的官网:mariadb官网,下载地址:下载地址。里面有国内源可以选择下载,最新版本的安装包体积较大,快1G了,使用国内源下载速度更快。当前最新稳定版是MariaDB 10.4 Series,使用清华大学源的下载地址为:

https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.4.8/bintar-linux-systemd-x86_64/mariadb-10.4.8-linux-systemd-x86_64.tar.gz

首先把安装包下载到本地,然后解压到/usr/local目录:

tar -zxvf mariadb-10.4.8-linux-systemd-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mariadb-10.4.8-linux-systemd-x86_64/ /usr/local/mysql

初始化数据库,设定数据存储目录为/appdata/mysql,启动用户为mysql

# 创建mysql用户
useradd -s /sbin/nologin -M mysql
# 创建数据库文件夹
mkdir /appdata/mysql -p
# 初始化数据库
/usr/local/mysql/scripts/mysql_install_db \
    --basedir=/usr/local/mysql \
    --datadir=/appdata/mysql \
    --user=mysql

初始化数据库的过程中如果报错:

> Installing MariaDB/MySQL system tables in '/xxx/mariadb' ...
/usr/local/mariadb/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
 
Installation of system tables failed!  Examine the logs in
/udata/mariadb for more information.
 
...

说明系统缺少组件库libaio,需要安装手动安装:

yum install libaio libaio-devel

执行成功后输出:

Installing MariaDB/MySQL system tables in '/appdata/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

...

到这里数据库就已经安装完成了,接下来要做的就是配置。

二、配置

修改my.cnf,设置pid/socket/log等文件的路径,把它们统一存到/appdata/mysql/run/下:

[mysqld]
datadir=/appdata/mysql
socket=/appdata/mysql/run/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/appdata/mysql/run/mysql.log
pid-file=/appdata/mysql/run/mysql.pid

[mysql]
socket=/appdata/mysql/run/mysql.sock

[mysqladmin]
socket=/appdata/mysql/run/mysql.sock

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

注意:

  1. /appdata/mysql/run目录要提前创建
  2. 如果修改了socket的路径,注意还要修改[mysql]和[mysqladmin]段的socket路径,要和[mysqld]中的socket路径一致

设置路径权限:

chown mysql.mysql -R /usr/local/mysql /appdata/mysql

添加mysql命令到系统路径,修改/etc/profile文件:

MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin

修改后source /etc/profile生效。

三、添加系统服务

3.1 service系统服务

对于使用service命令启动的服务,复制mysql主目录下的support/mysql.server文件到/etc/init.d/

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

然后修改文件中的配置:

basedir=/usr/local/mysql # 安装目录
datadir=/appdata/mysql # 数据目录
mysqld_pid_file_path=/appdata/mysql/run/mysql.pid # pid文件目录

注意:配置要和上面my.cnf中的配置一一对应

启动:service mysqld start

添加到开机启动:

chkconfig --add mysqld
chkconfig mysqld on

3.2 systemd系统服务

systemd服务的文件在安装路径/support-files/systemd/mariadb.service

cp support-files/systemd/mariadb.service /etc/systemd/system/mysqld.service

复制完后执行systemctl start mysqld启动服务,然后设置开机启动:

systemctl enable mysqld

四、设置root用户密码

系统服务起来后,可以使用mysqladmin初始化root用户的密码:

mysqladmin -u root password '123456'

登陆:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
最后修改:2019 年 09 月 27 日 08 : 41 PM

发表评论