Loading
0

Linux系统yum安装MySql/MariaDB数据库及mysql_secure_installation安全配置详解
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器
前言:之前用户反馈安装了MariaDB数据库找不到root密码,查看了传统的mysql数据库日志也没有记录root密码,经过查找资料,MariaDB数据库安装后设置数据库的root密码需要通过 mysql_secure_installation 命令来设置以及配置安全选项,网上有很多教程,本教程主要是方便自己查找防止忘记记录一下,如下教程将介绍“MariaDB”及“MySql”数据库的安装及mysql_secure_installation 安全设置
说明:
若服务器之前有安装过数据库有重要数据,请提前导出备份一下,以免数据覆盖无法找回。
1、该方法也适用于MySql数据库。
2、本教程所使用的是CentOS8系统,目前CentOS8官方已经停止维护,yum源已经无法使用,解决方法可参考如下方法处理后即可
     非阿里云服务器修改yum源https://www.zfcdn.xyz/showinfo-3-36185-0.html
     阿里云服务器修改yum源https://www.zfcdn.xyz/showinfo-3-36184-0.html
安装方法:
1、安装MariaDBMySql数据库方法
安装MariaDB数据库方法:

yum install -y mariadb-server mariadb*

若您使用的是MySql数据库可使用如下命令安装:

yum install mysql-community-server mysql*

2、yum安装完成后启动数据库:
MariaDB数据库启动使用如下命令:

service mariadb start

MySql数据库启动命令:

service mysql start

或者

service mysqld start

启动后使用命令 netstat -lunpt 可以查看到3306被监听,则表示启动正常,如下

[root@blog-tag-gg ~]# netstat -lunpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22000           0.0.0.0:*               LISTEN      899/sshd            
tcp6       0      0 :::3306                 :::*                    LISTEN      17334/mysqld        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           720/chronyd         
udp6       0      0 ::1:323                 :::*                                720/chronyd

3、安装完成后需要设置root密码及安全配置,执行 mysql_secure_installation 命令后显示如下,配置有说明,可参考:

[root@blog-tag-gg ~]# 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):    <– 默认会提示输入数据库的root密码,若没设置密码请直接回车
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)   <– 上面我输入了错误的root密码所以提示无法登录
Enter current password for root (enter for none): <–没设置数据库的root密码直接回车
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   <– 提示设置数据库的root密码,输入y回车设置,请根据自己情况选择。
New password: <–输入数据库root密码
Re-enter new password: <– 再次输入数据库的root密码
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  <–提示您是否要删除匿名用户,若是生成环境,建议删除,以增强安全,输入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远程登录权限,若您的程序使用127.0.0.1本机调用数据库且不需要服务器以外的地方调用,建议输入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  <– 提示您是否删除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  <– 提示您是否刷新权限表,上面做了这么设置,可以刷新一下,输入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!   <– 设置完成。

4、若在服务器内部使用数据库root账号登录数据库,可使用 mysql -uroot -p 命令登录,如下

[root@blog-tag-gg ~]# mysql -uroot -p
Enter password: <– 输入数据库的root密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.3.28-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)]>  <– 若显示这里,则说明成功登录了。








 
301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-13-36186-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?
上一篇:MySQL启动报:mysqld:Incorrect information in file:/blogtaggg/test.frm解决方法
下一篇:MySQL数据库启用binlog日志方法及查看是否开启binlog日志