天天看點

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      

繼續閱讀