安装MySQL数据库并去除安全隐患

273 阅读2分钟

部署环境

3A云服务器:centos 7.6

1. 安装完mariadb-server后,运行mysql_secure_installation去除安全隐患

mysql_secure_installation会执行几个设置:

(1)

为root用户设置密码

(2)

删除匿名账号

(3)

取消root用户进程登录

[root@xuegod63 ~]# rpm -qf /usr/bin/mysql_secure_installation #查看此命令的安装包

mariadb-server-5.5.56-2.el7.x86_64

[root@xuegod63 ~]# mysql_secure_installation

#迚入安全配置导向

通过这几项的设置能够提高MySQL库的安全。建议生产环境中MySQL安装这完成后一定要运

行一次mysql_secure_installation,详细步骤请参看下面的命令:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current

password for the root user. If you've just installed MySQL, 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):

# 初次运行直接回车,因为root用户没有

密码

OK, successfully used password, moving on

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 #是否删除test数据库,直接回车戒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!

… Success!

2. 数据库连接

连接MariaDB数据库,连接本机可以去掉 -h选项

# mysql

#如MariaDB没有设置密码,并在本地上登录,而且是使用默认端口的情况下,

可以直接使用mysql命令登录。

戒:

# mysql -h IP -u USER -pPASS

#如 MariaDB丌是安装在本地时,需要使用-h选项指定IP

登录,如 MariaDB用户设置了密码时,需要使用-u选项指定用户,并使用-p选项指定密码,注意的是在

-p选项和密码之间丌能有空栺,否则在命令执行后需要再次输入密码,并将有空栺的密码串识别为登录的

数据库。

3. 测试数据是否正常

[root@xuegod63 ~]# mysql -u root -p123456

MariaDB [(none)]> show databases; #没有test数据库 #执行时,所有命令以;号结尾

+-------------------------+

| Database

|

+-------------------------+

| information_schema |

| mysql

|

| ucenter

|

+-------------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]> exit

#退出命令可以加分号,也可以丌加分号。

4. 测试网站是否支持PHP

[root@xuegod63 ~]# cd /var/www/html/

[root@xuegod63 html]# vim index.php

<?php

phpinfo();

?>

[root@xuegod63 html]# systemctl restart httpd

#重启httpd服务。

[root@xuegod63 html]# iptables -F #清空防火墙

测试:

http://192.168.1.63/index.php

image.png