centos7.6二進制安裝mariadb10.4.11

寫在前面

mariadb是mysql的一個分支,由MySQL社區進行維護更新. 在這裏記錄我的一次安裝過程.
安裝libaio庫文件
yum install libaio* -y
下載二進制包(mariadb10.4.11下載列表)

注意: 要選擇符合自己服務器的操作系統,以及拓展(liunx裏可以使用 uname -a 查看是64位還是32位),鏡像的話選擇:清華大學TUNA協會(清華大學TUNA協會)

安裝

因爲二進制包是編譯好的,我們需要做的就是初始化環境,比如創建mysql賬戶,創建數據存儲的目錄等.
附上官方提供的幫助手冊:
https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/

創建mysql賬戶

官方編譯時,默認指定了運行數據庫程序的用戶爲:mysql. 我們需要創建mysql賬戶

useradd mysql
創建mariadb程序目錄

官方編譯時,默認指定的Mariadb的目錄爲:/usr/local/mysql. 我們可以把壓縮包解壓至:/usr/local/mysql,不過更通用的辦法是建立一個軟連接:

tar -zxvf   mariadb-10.4.10-linux-x86_64.tar.gz   #解壓mariadb包

ln -s mariadb-10.4.10-linux-x86_64 /usr/local/mysql -r   建立軟連接
初始化數據庫

假如以/data/mariadb_data 作爲數據庫存放數據的地方

mkdir /data/mariadb_data -p

前面說了,官方編譯時,指的的運行用戶是mysql,所以我們剛纔創建的目錄,mysql需要有讀寫權限,我們直接把屬組屬主改爲mysql:

chown root.root /data/mariadb_data -R

進入到mysql目錄cd /usr/local/mysql,執行以下命令:

./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mariadb_data
Installing MariaDB/MySQL system tables in '/data/mariadb_data' ...
OK

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

--user 是用mysql用戶運行
--basedir=是mysql程序的位置
--datadir=是打算讓數據存放的位置

初始化完成以後,查看ls /data/mysql/能查看到初始化以後的數據庫。

關於mariadb的配置文件

在官方配置好的mariadb讀取配置文件有順序之分。
讀取順序:/etc/my.cnf > /etc/mysql/my.cnf > /usr/etc/my.cnf > ~/.my.cnf
一般來講,前2個/etc/my.cnf後者/etc/mysql/my.cnf 比較常用,如果配置文件位置多了,反而不好管理,另外一種常用的配置就是在啓動mysql時,給它指定好配置文件。

另外需要注意的是,有可能你的舊配置文件/etc/my.cnf存在,這樣可能會影響到數據庫的配置,此時需要保證該配置文件的配置有效性。

vim /etc/my.cnf

[mysqld]
datadir=/data/mariadb_data
socket=/data/mariadb_data/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=/data/mariadb_data/log/mariadb.log
pid-file=/data/mariadb_data/pid/mariadb.pid

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

根據此配置文件,我還需要在/data/mysql下創建2個目錄,用於保存日誌和pid文件

mkdir /data/mariadb_data/{log,pid}
chown mysql.mysql -R /data/mariadb_data
安全初始化
 /usr/local/mysql/bin/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):               #第一次運行直接回車
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] n          #您的根帳戶已受保護,因此可以安全地回答“n”。
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] 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         #是否刪除匿名用戶
 ... 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         #是否禁止root遠程登錄
 ... 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         #是否刪除測試數據庫
 - 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        #是否刷新權限
 ... 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!

 
注意:運行mysql_secure_installation有個小坑,官方在編譯的時候,指定了它連接的socket文件是在/tmp目錄下的,所以可以對socket文件做個軟連接到/tmp目錄下,這樣可以解決找不到socket文件的問題。
啓動mariadb
/usr/local/mysql/bin/mysqld_safe &                               #後臺啓動mariadb服務
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown              #停止mariadb服務

寫在後面

上述的安裝過程在本地啓動mysql時,因爲使用的是sock通訊方式,所以可以直接使用/usr/local/mysql/bin/mysql 進入數據庫.但是用navicat等工具的ssh鏈接時,還是要輸入密碼的.

或者本地鏈接時,帶上 -h 127.0.0.1,如/usr/local/mysql/bin/mysql -uroot -p -h 127.0.0.1,這樣就需要輸入正確的賬號密碼才能登錄mariadb.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章