#systemctl start mariadb验证systemctl状态以确保mariadb数据库服务器已成功启动
# systemctl status mariadb
? mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2017-06-26 18:26:35 UTC; 13s ago
Process: 4049 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 3969 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 4048 (mysqld_safe)
CGroup: /system.slice/mariadb.service
+-4048 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
+-4206 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/ma...
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: The latest information about MariaDB is available at http://mariadb.org/.
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: You can find additional information about the MySQL part at:
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: http://dev.mysql.com
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: Support MariaDB development by buying support/new features from MariaDB
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: Corporation Ab. You can contact us about this at sales@mariadb.com.
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: Alternatively consider joining our community based development effort:
Aug 15 15:20:30 deploy mariadb-prepare-db-dir[3969]: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Aug 15 15:20:30 deploy mysqld_safe[4048]: 170601 15:20:33 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Aug 15 15:20:30 deploy mysqld_safe[4048]: 170601 15:20:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Aug 15 15:20:35 deploy systemd[1]: Started MariaDB database server.
4.连接并验证MariaDB Server
使用如下所示的mysql命令使用mysql的root用户连接到数据库
# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
以下show database命令将显示默认的mysql数据库。
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5.执行MariaDB Post Installation步骤
从上方可以看出,默认情况下,安装不会为MySQL root帐户分配任何密码。
要设置mysql root用户密码并在数据库上执行其他安全配置,请执行如下所示的mysql_secure_installation脚本。
# /usr/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.
由于这是我们第一次运行此脚本,因此没有为mysql root帐户分配密码。所以,请在这里输入
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.
在这个阶段,说“y”为MySQL根帐户分配密码。之后输入密码。
请注意,这个mysql root帐户与linux root帐户不同。所以,我们在这里设置mysql root帐号的密码,这与Linux root帐号无关。
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!作为默认安装的一部分,mysql安装匿名用户,无需真实用户即可登录数据库。所以我们应该删除这个用户
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.
这是默认的测试数据库,我们应该删除它。
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
在这里输入y,以确保我们所做的所有更改将立即生效。
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
6.验证MySQL根访问
现在,如果您连接到没有root密码的Mysql,您将收到以下访问被拒绝的错误消息。
# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
要指定密码,请使用-p选项,如下所示。这将提示用户输入密码。
# mysql -u root -p
Enter password:另外,从下面的show databases命令可以看出,测试数据库现在被删除了。
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+如果要在mysql命令行中传递密码,请在-p选项旁边指定,如下所示。
# mysql -u root -pMySecurePassword注意:-p和密码之间没有空格。这可能会导致一些混淆,因为我们在-u和username之间有空格。但是,-p和密码之间没有空格。