mariadb是一個(gè)開源數(shù)的據(jù)庫(kù)管理軟件,是MySQL的一個(gè)分支,是MySQL創(chuàng)始人在出售MySQL之后開發(fā)的一個(gè)新的數(shù)據(jù)庫(kù)軟件,在MySQL的基礎(chǔ)上進(jìn)行了新的優(yōu)化。
準(zhǔn)備工作
安裝mariadb的方式有多種,在這里介紹一下mariadb的二進(jìn)制安裝方式,首先要現(xiàn)在mariadb的二進(jìn)制包,推薦官網(wǎng)下載https://downloads.mariadb.org/mariadb/10.2.8/,在這里下載最新的10.2.8穩(wěn)定版
把已經(jīng)下載好的二進(jìn)制包mariadb-10.2.8-linux-x86_64.tar上傳至虛擬機(jī)內(nèi)
[root@c7 ~]#rz
[root@c7 ~]#ll
total 445180
-rw-r--r-- 1 root root 455856309 Sep 21 09:43 mariadb-10.2.8-linux-x86_64.tar.gz
準(zhǔn)備mysql用戶和家目錄
[root@c7 ~]#getent passwd mysql --------------------------------------- # 查看mysql用戶是否存在
[root@c7 ~]#useradd -r -m -d /app/dbdata -s /sbin/nologin mysql ------- # 創(chuàng)建mysql用戶,指定家目錄和用戶類型
[root@c7 ~]#getent passwd mysql --------------------------------------- # 確認(rèn)創(chuàng)建mysql用戶
mysql:x:989:984::/app/dbdata:/sbin/nologin
[root@c7 ~]#ll /app/ -------------------------------------------------- # 確認(rèn)創(chuàng)建mysql用戶家目錄
drwx------ 3 mysql mysql 78 Sep 25 14:46 dbdata
解壓二進(jìn)制包并安裝
二進(jìn)制包是編譯過后的程序,不是源碼,在編譯過程中已經(jīng)指定過安裝的路徑,必須安裝編譯的路徑去解壓存放。mariadb默認(rèn)安裝在/usr /local/mysql/文件夾下,在解壓時(shí)直接指定解壓在/usr/local/文件夾下
[root@c7 ~]#tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/
[root@c7 ~]#ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
drwxrwxr-x 12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Sep 9 20:56 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
由此可見,因?yàn)榻鈮汉蟮奈募A名稱和報(bào)名相同,而不是mariadb安裝的文件夾mysql,在這里可以把文件夾名稱改為mysql或者創(chuàng)建一個(gè)軟連接
[root@c7 local]#ln -sv mariadb-10.2.8-linux-x86_64/ mysql
‘mysql’ -> ‘mariadb-10.2.8-linux-x86_64/’
[root@c7 local]#ll
total 0
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
drwxrwxr-x 12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
lrwxrwxrwx 1 root root 28 Sep 25 15:04 mysql -> mariadb-10.2.8-linux-x86_64/
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Sep 9 20:56 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
準(zhǔn)備配置文件
配置格式:類ini格式,各程序由單個(gè)配置文件提供配[prog_name]
配置文件查找次序:后面覆蓋前面的配置文件
/etc/my.cnf--> /etc/mysql/my.cnf--> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
雖然現(xiàn)在還沒安裝配置文件,但/etc/my.cnf已經(jīng)生產(chǎn),在這里我們可以創(chuàng)建一個(gè)優(yōu)先級(jí)更高的的/etc/mysql/my,cnf文件。在整理可以用support-files/my-huge.cnf為模板,復(fù)制并改名為my.cnf
[root@c7 mysql]#mkdir /etc/mysql
[root@c7 mysql]#ll /etc/mysql/
total 0
[root@c7 mysql]#cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@c7 mysql]#ll /etc/mysql/my.cnf
-rw-r--r-- 1 root root 4901 Sep 25 15:36 /etc/mysql/my.cnf
修改配置文件,添加必要的修改信息
[root@c7 mysql]#vim /etc/mysql/my.cnf
......
# The MySQL server
[mysqld]
port = 3306
datadir = /app/dbdata -------------- # 數(shù)據(jù)庫(kù)目錄
innodb_file_per_table = on --------- # 生成獨(dú)立的數(shù)據(jù)庫(kù)文件
skip_name_resolve = on ------------- # 禁止主機(jī)名解析
創(chuàng)建日志文件
在mariadb的配置文件中,默認(rèn)日志文件存在于/var/log/mariadb/maria.log,這個(gè)文件需要手動(dòng)去創(chuàng)建,并且修改用戶為mysql
[root@c7 mysql]#mkdir /var/log/mariadb
[root@c7 mysql]#touch /var/log/mariadb/maria.log
[root@c7 mysql]#chown -R mysql:mysql /var/log/mariadb/
[root@c7 mysql]#ll /var/log/mariadb/
total 0
-rw-r--r-- 1 mysql mysql 0 Sep 25 17:19 maria.log
[root@c7 mysql]#ll /var/log/mariadb/ -d
drwxr-xr-x 2 mysql mysql 23 Sep 25 17:19 /var/log/mariadb/
創(chuàng)建系統(tǒng)數(shù)據(jù)庫(kù)
在系統(tǒng)中存在mysql數(shù)據(jù)庫(kù)和text數(shù)據(jù)庫(kù),mysql數(shù)據(jù)庫(kù)是系統(tǒng)數(shù)據(jù)庫(kù),在/app/dbdata/文件夾中還沒有創(chuàng)建
[root@c7 mysql]#ll /app/dbdata/
total 0
執(zhí)行腳本文件scripts/mysql_install_db創(chuàng)建系統(tǒng)數(shù)據(jù)庫(kù),這個(gè)腳本只能在/usr/local/mysql/下執(zhí)行。否則會(huì)報(bào)錯(cuò)。在執(zhí)行此腳本時(shí)需指定數(shù)據(jù)庫(kù)目錄和運(yùn)行者身份。
[root@c7 mysql]#scripts/mysql_install_db --datadir=/app/dbdata --user=mysql
......
[root@c7 mysql]#ls /app/dbdata/
aria_log.00000001 ibdata1 mysql mysql-bin.state
aria_log_control ib_logfile0 mysql-bin.000001 performance_schema
ib_buffer_pool ib_logfile1 mysql-bin.index test
此時(shí)系統(tǒng)數(shù)據(jù)庫(kù)mysql和text已經(jīng)創(chuàng)建成功,一個(gè)文件夾代表一個(gè)數(shù)據(jù)庫(kù)
啟動(dòng)數(shù)據(jù)庫(kù)
復(fù)制文件夾support-files下的啟動(dòng)文件mysql.server到 /etc/init.d/并改名mysqld,啟動(dòng)mysqld,并查看啟動(dòng)后的進(jìn)程
[root@c7 mysql]#cp support-files/mysql.server /etc/init.d/mysqld
[root@c7 mysql]#chkconfig mysqld on
[root@c7 mysql]#chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@c7 mysql]#service mysqld start
Starting mysqld (via systemctl): [ OK ]
配置環(huán)境變量
此時(shí)mysqld對(duì)應(yīng)的端口3306已經(jīng)打開,但是使用mysql依然無法進(jìn)入數(shù)據(jù)庫(kù),再次需要配置環(huán)境變量
[root@c7 mysql]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
[root@c7 mysql]#mysql
bash: mysql: command not found...
在etc下編輯變量配置文件,執(zhí)行新的環(huán)境變量文件,重新登錄mysql
[root@c7 bin]#vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@c7 bin]#. /etc/profile.d/mysql.sh
[root@c7 bin]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
初始化數(shù)據(jù)庫(kù)
剛建立的數(shù)據(jù)庫(kù)沒有進(jìn)行任何的加密,用戶可以隨意登錄,起不到保護(hù)數(shù)據(jù)的作用,因此需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化設(shè)置
[root@c7 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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): ------------------------------ # 沒有設(shè)置密碼,直接回車
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y ---------------------------------------------------- # 設(shè)置密碼
New password: ----------------------------------------------------------------- # 輸入密碼
Re-enter new password: -------------------------------------------------------- # 確認(rèn)密碼
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] n ---------------------------------------- # 不允許root遠(yuǎn)程登錄
... skipping.
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] n ------------------------------- # 不刪除test測(cè)試數(shù)據(jù)庫(kù)
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y ---------------------------------------- # 重新加載數(shù)據(jù)庫(kù)
... Success!
Cleaning up...
此時(shí)數(shù)據(jù)庫(kù)已經(jīng)不能使用mysql命令直接連接,如要指定用戶,并輸入密碼才能連接
[root@c7 ~]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@c7 ~]#mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>