天天看点

Linux安装MariaDB(超详细的yum安装、二进制安装)

分享知识 传递快乐

1、yum安装

官方网站yum配置方法链接:​​https://mariadb.com/kb/en/library/yum/​​

1)配置yum源(安装不同版本需要配置不同源)

为了更好的管理MariaDB版本,创建一个新源文件:

[guest@localhost ~]$ cd /etc/yum.repos.d/
[guest@localhost yum.repos.d]$ sudo vim MariaDB.repo      

打开文件后并切换到编辑模式。

1.1)使用MariaDB存储库配置工具

如果要使用存储库在CentOS 7上安装MariaDB 10.3,则可以在/etc/yum.repos.d/MariaDB.repo中使用以下yum存储库配置:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1      

1.2)将MariaDB存储库固定到特定的版本

如果希望将yum存储库固定到特定的版本,或者要将yum降级到特定的版本,则可以使用以下yum存储库配置:

[mariadb]
name = MariaDB-10.3.14
baseurl=http://yum.mariadb.org/10.3.14/centos7-amd64
# alternative: baseurl=http://archive.mariadb.org/mariadb-10.3.14/yum/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1      

注意:如果更改现有存储库配置,则需要执行以下操作:

[guest@localhost yum.repos.d]$ sudo yum clean all
[guest@localhost yum.repos.d]$ sudo yum makecache      

2)安装

[guest@localhost yum.repos.d]$ sudo yum install mariadb      

3)启动服务

[guest@localhost ~]$ sudo systemctl enable mariadb
[guest@localhost ~]$ sudo systemctl start mariadb      

2、二进制安装

1)官网下载二进制包

下载地址:​​https://downloads.mariadb.org/mariadb/10.4.10/​​

文件名:mariadb-10.4.10-linux-systemd-x86_64.tar.gz (for systems with systemd)

2)MariaDB安装路径及权限分配

2.1)解压tar.gz文件到指定目录下

[root@localhost ~]# mkdir /opt/mariadb
[root@localhost ~]# tar -zxvf mariadb-10.4.10-linux-systemd-x86_64.tar.gz -C /opt/mariadb      

2.2)修改文件夹权限

把MariaDB安装在了 /opt/mariadb/10.4.10 目录下并修改文件权限:

[root@localhost ~]# cd /opt/mariadb/
[root@localhost mariadb]# mv mariadb-10.4.10-linux-systemd-x86_64/ 10.4.10
[root@localhost mariadb]# chown -R root:root 10.4.10/      

在什么目录安装则在什么目录下操作即可。

3)创建mysql用户

根据文件我们要创建一个mysql的用户,它可以对以后的mysql数据库进行管理,同时我们还可以指定mysql的数据目录,这样以后它的存储数据就可以独立出来放置了。

创建非登陆的mysql用户

[root@localhost mariadb]# useradd -s /sbin/nologin -d /opt/lnmp/bin/mariadb mysql      

可以查看所有用户的列表

[root@localhost mariadb]# cat /etc/passwd      

删除用户

[root@localhost mariadb]# userdel mysql      

4)创建数据库路径并分配mysql权限

[root@localhost mariadb]# mkdir database
[root@localhost mariadb]# chown -R mysql:mysql database/      

5)修改配置文件

安装或配置MySQL或MariaDB需要用到一个my.cnf的文件,my.cnf是mysql启动时加载的配置文件,一般会放在mysql的安装目录中,用户也可以放在其他目录加载。总的来说my.cnf类似与window中my.ini。在某此系统上为 “/etc/my.cnf”、“/etc/mysql/my.cnf”、“ ~/.my.cnf”。

以下是本人my.cnf下的所有配置信息:

[root@localhost mariadb]# cd /etc/
[root@localhost etc]# vim my.cnf

[mysqld]
# datadir=/var/lib/mysql
datadir=/opt/mariadb/database
socket=/var/lib/mysql/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=/var/log/mariadb/mariadb.log
log-error=/opt/mariadb/10.4.10/logs/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

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

解析:

  • datadir:指向的是数据库路径
  • log-error:指向数据库日志路径

这里保修改了 datadir、log-error 两个配置路径,其余配置均是默认配置。此外还可以配置多项配置,在这里就不过多说明了。

配置完成后还需要创建配置文件中的指定路径及权限问题:

[root@localhost database]# mkdir /var/lib/mysql
[root@localhost database]# chown -R mysql:mysql /var/lib/mysql
[root@localhost database]# touch /var/lib/mysql/mysql.sock
[root@localhost mysql]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 15 13:11 mysql.sock
[root@localhost mysql]# chmod guo+wr mysql.sock 
[root@localhost mysql]# ll
总用量 0
-rw-rw-rw-. 1 root root 0 12月 15 13:11 mysql.sock
[root@localhost database]# 
[root@localhost database]# 
[root@localhost database]# 
[root@localhost database]# mkdir /opt/mariadb/10.4.10/logs
[root@localhost database]# touch /opt/mariadb/10.4.10/logs/mariadb.log
[root@localhost database]# 
[root@localhost database]# 
[root@localhost database]# 
[root@localhost database]# mkdir /var/run/mariadb
[root@localhost database]# chown -R mysql:mysql /var/run/mariadb
[root@localhost database]# touch /var/run/mariadb/mariadb.pid
[root@localhost database]# cd /var/run/mariadb/
[root@localhost mariadb]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 15 13:12 mariadb.pid
[root@localhost mariadb]# chmod guo+wr mariadb.pid 
[root@localhost mariadb]# ll
总用量 0
-rw-rw-rw-. 1 root root 0 12月 15 13:12 mariadb.pid      

5)初始化数据库

[root@localhost 10.4.10]# ./scripts/mysql_install_db --user=mysql --datadir=/opt/mariadb/database      

6)启动数据库

6.1)命令启动

启动数据库

[root@localhost 10.4.10]# ./bin/mysqld_safe --defaults-file=/etc/my.cnf &      

关闭数据库

[root@localhost 10.4.10]# ./bin/mysqladmin -uroot -p shutdown      

查看mariadb服务状态:

[root@localhost 10.4.10]# ps aux | grep mariadb      

6.2)服务启动

(1)默认配置

使用MariaDB默认路径默认配置必须安装在 /usr/local/mysql 的目录下。

如果配置在其它的目录下使用默认配置,需要创建软连接:

[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -s /opt/mariadb/10.4.10/ mysql      

(2)自定义配置

使用自定义配置时需要修改 support-files/mysql.server 文件:

basedir=/opt/mariadb/10.4.10
datadir=/opt/mariadb/database      

创建服务

在 /etc/init.d/ 目录下创建软连接指向 mysql.server 或把 mysql.server 移动到 /etc/init.d/ 目录下,修改为 mysql :

[root@localhost 10.4.10]#

[root@localhost 10.4.10]# cp support-files/mysql.server /etc/init.d/mysql
# 或(二选其一即可)
# [root@localhost 10.4.10]# ln -s support-files/mysql.server /etc/init.d/mysql

[root@localhost 10.4.10]# cd /etc/init.d/
[root@localhost 10.4.10]# chkconfig --add mysql
[root@localhost 10.4.10]# chkconfig --list mysql

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysql           0:关 1:关 2:开 3:开 4:开 5:开 6:关      

查询所有服务

[root@localhost 10.4.10]# chkconfig --list      

删除服务

[root@localhost 10.4.10]# chkconfig --del mysql      

启动服务

[root@localhost init.d]# service mysql start      

关闭服务

[root@localhost init.d]# service mysql stop      

查看状态

[root@localhost init.d]# service mysql status      

7)连接、查询数据库

7.1)连接与登录

[root@localhost 10.4.10]# ./bin/mysql -uroot -p
Enter password:      

7.2)查询

MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host,user,password from user;      

8)修改连接密码

MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql;
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY "admini";      

修改完成后,重新启动数据库即可。

3、安装时遇见异常

异常一:Mariadb日志打印异常

启动MariaDB时,mariadb.log日志出现以下错误:

[Note] Server socket created on IP: '::'.
[ERROR] Can't start server : Bind on unix socket: Address already in use
[ERROR] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?
[ERROR] Aborting      

问题原因:文件不存在或没有读写权限问题。

解决办法:

  • 检查 /var/lib/mysql/mysql.sock  目录或文件是否存在,如果没存在则创建;
  • 检查 /var/lib/mysql 目录及 mysql.sock 文件是否有读写权限,如果没有则添加读写权限。

异常二:Mariadb日志打印异常

启动MariaDB时,mariadb.log日志出现以下错误:

[Note] Plugin 'FEEDBACK' is disabled.
[Note] InnoDB: Buffer pool(s) load completed at 191215 13:24:44
[ERROR] /opt/mariadb/10.4.10/bin/mysqld: unknown variable 'defaults-file=/etc/my.cnf'
[ERROR] Aborting      

问题原因:无效的defaults-file变量或找不到/etc/my.cnf文件。

解决办法:

  • 检查mysqld_safe命令的defaults-file变量使用是否正确;
  • 检查defaults-file变量指向的文件地址是否正确或文件是否存在;

异常二:Mariadb日志打印异常

启动MariaDB时,mariadb.log日志出现以下错误:

[ERROR] mysqld: File '/opt/mariadb/database/aria_log_control' not found (Errcode: 13 "Permission denied")
[ERROR] mysqld: Got error 'Can't open file' when trying to use aria control file '/opt/mariadb/database/aria_log_control'
[ERROR] Plugin 'Aria' init function returned error.
[ERROR] Plugin 'Aria' registration as a STORAGE ENGINE failed.
[Note] InnoDB: Using Linux native AIO
[ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
[ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[Note] Plugin 'FEEDBACK' is disabled.
[ERROR] Could not open mysql.plugin table. Some plugins may be not loaded
[ERROR] Failed to initialize plugins.
[ERROR] Aborting      

问题原因:没有操作权限,这是以非root身份启动时出现在异常。

解决办法:

  • 切换到root下执行。

异常二:连接数据时异常

[root@localhost 10.4.10]# ./bin/mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)      

问题原因:连接本地MySQL时出现的套接字异常。

解决办法:

在/tmp文件夹下创建mysql.sock文件或创建一个软连接:

[root@localhost 10.4.10]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock      

建议创建软连接的方式。

异常四:初始化数据库异常

[root@contos7 10.4.11]# ./scripts/mysql_install_db --defaults-file=/etc/my.cnf
Installing MariaDB/MySQL system tables in '/opt/lnmp/bin/mariadb/database' ...
./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
/opt/lnmp/bin/mariadb/database for more information.

The problem could be conflicting information in an external
my.cnf files. You can ignore these by doing:

    shell> ./scripts/mysql_install_db --defaults-file=~/.my.cnf

You can also try to start the mysqld daemon with:

    shell> ./bin/mysqld --skip-grant-tables --general-log &

and use the command line tool ./bin/mysql
to connect to the mysql database and look at the grant tables:

    shell> ./bin/mysql -u root mysql
    mysql> show tables;

Try 'mysqld --help' if you have problems with paths.  Using
--general-log gives you a log in /opt/lnmp/bin/mariadb/database that may be helpful.

The latest information about mysql_install_db is available at
https://mariadb.com/kb/en/installing-system-tables-mysql_install_db
You can find the latest source at https://downloads.mariadb.org and
the maria-discuss email list at https://launchpad.net/~maria-discuss

Please check all of the above before submitting a bug report
at http://mariadb.org/jira

[root@contos7 10.4.11]#      

问题原因:系统缺少安装包

解决办法:

安装 libaio 包,安装命令:

yum install libaio      

继续阅读