原本題目不是要求要安裝 MySQL 嗎?怎麼裝的是 MariaDB?
這得講一下歷史故事了。詳細內容可見鐵人賽前輩的文章([Day 13] 開始進入資料庫篇,淺談MySql與MariaDB)。簡單來說,MariaDB 就是 MySQL 的 fork,MySQL 之於 MariaDB 的關係,就像是 Red Hat Enterprise Linux (RHEL) 之於 CentOS 的關係。自從 MySQL 變成 Oracle 甲骨文公司的產品後,MySQL 就已經從 RHEL 及 CentOS 所提供的套件清單中移出了。
那 …… 我還是想要安裝 MySQL 怎麼辦?→留待下一篇文章做說明。
安裝 mariadb 與 mariadb-server 套件
$ sudo yum groupinstall mariadb mariasb-client -y
說明:以上是 Red Hat Enterprise Linux 7 RH254 Red Hat System Administration III Edition 1 Student Workbook 第 250 頁的做法,但如果是 Red Hat Enterprise Linux 7 System Administrator's Guide 第 17 章 Database Servers 的做法則是
$ sudo yum install mariadb mariadb-server -y
啟動 MariaDB 服務
$ sudo systemctl start mariadb
啟用 MariaDB 服務,讓 MariaDB 服務每次開機就會自動被帶起來
$ sudo systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
檢視 MariaDB 服務啟動狀態
$ sudo systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-09-10 10:05:12 CST; 4min 57s ago
Main PID: 2067 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─2067 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─2229 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
Sep 10 10:05:10 centos7-cli.lab.example.com systemd[1]: Starting MariaDB database server...
Sep 10 10:05:10 centos7-cli.lab.example.com mariadb-prepare-db-dir[2035]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Sep 10 10:05:10 centos7-cli.lab.example.com mariadb-prepare-db-dir[2035]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
Sep 10 10:05:10 centos7-cli.lab.example.com mysqld_safe[2067]: 190910 10:05:10 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Sep 10 10:05:10 centos7-cli.lab.example.com mysqld_safe[2067]: 190910 10:05:10 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Sep 10 10:05:12 centos7-cli.lab.example.com systemd[1]: Started MariaDB database server.
加強 MariaDB 安裝的安全性
$ sudo 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):
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
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
... 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 -V
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
登入資料庫進行簡單測試
$ mysql -u root -h localhost -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye