1.数据库的介绍

1.1 数据是什么?

密码账户、图片、视频、数字,链接都是人类可以解除的数据。

机器语言二进制语言是计算机了解的数据。

1.2 帐号密码如何存储?

如果将一个网站的文档存在一个普通文档中:可能被误删除,文件也不安全;大数据量的时候可能存储限制;查询用户也很麻烦。

所以我们使用数据库管理系统(DBMS)。

关系型数据库(RDBMS):比较适合于,安全级别要求高的数据以及关系较复杂的数据:Oracle,Mysql

非关系型数据库:适合于高性能存取数据,一般是配合RDBMS进行使用的,针对大数据处理分析,分布式架构更加擅长:MongoDB(文档型)、Redis

1.3 什么是数据库?

数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进行新增、截取、更新、删除等操作。

所谓“数据库”是以一定方式储存在一起、能与多个用户共享、具有尽可能小的冗余度、与应用程序彼此独立的数据集合

数据对一个企业是非常重要的,其实就相当于我们每个人的信息就是自己的属性,并且把它记录在一个文件里面
多张表格组成一个数据库,tab (表)---->db (库)----->数据库软件

1.4 什么是mariadb数据库?

MariaDB是MySQL源代码的一个分支。

MariaDB由MySQL的创始人Michael Widenius主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL AB卖给了SUN。此后,随着SUN被甲骨文收购,MySQL的所有权也落入Oracle的手中,MariaDB名称来自Michael Widenius的女儿Maria的名字。

MariaDB不仅仅是Mysql的一个替代品,MariaDB包括的一些新特性使它优于MySQL

MariaDB跟MySQL在绝大多数方面是兼容的。在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB。

2.安装安全的mariadb。

2.1 安装

实验准备:

  • 一台配置好yum仓库的linux虚拟机。

1)直接进行yum安装:

[root@mariadb ~]# yum search mariadb # 查询所有mariadb有关的安装包
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
============================= N/S matched: mariadb =============================
mariadb-bench.x86_64 : MariaDB benchmark scripts and data
mariadb-devel.i686 : Files for development of MariaDB/MySQL applications
mariadb-devel.x86_64 : Files for development of MariaDB/MySQL applications
mariadb-libs.i686 : The shared libraries required for MariaDB/MySQL clients
mariadb-libs.x86_64 : The shared libraries required for MariaDB/MySQL clients
mariadb-server.x86_64 : The MariaDB server and related files
mariadb.x86_64 : A community developed branch of MySQL
mariadb-test.x86_64 : The test suite distributed with MariaD

  Name and summary matches only, use "search all" for everything.

[root@mariadb ~]# yum install mariadb-server -y #安装mariadb服务
Installed:
  mariadb-server.x86_64 1:5.5.52-1.el7                                          

Dependency Installed:
  mariadb.x86_64 1:5.5.52-1.el7       perl-DBD-MySQL.x86_64 0:4.023-5.el7      

Complete!

2)开启mariadb服务:

[root@mariadb ~]# systemctl start mariadb.service

3)输入mysql可以直接进入数据库查看:

[root@mariadb ~]# mysql
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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

直接查看数据库没有密码就可以进入,这是非常不安全的。

2.2 安全初始化

1)数据库起始状态设定信息是不安全的,需要做以下设定:

[root@mariadb ~]# 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!

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 # 进制远程登录root
 ... 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!

2)直接输入mysql无法登录:

[root@mariadb ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

3)输入密码进行测试登录:

[root@mariadb ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>[root@mariadb ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>

2.3 关闭网络端口

默认情况下,数据库的网络端口是打开的,为了安全需要关闭此接口:

1)查看数据库的网络端口是否开启:

[root@mariadb ~]# netstat -antlupe | grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      27         33752      4156/mysqld 

2)关闭网络端口(开着很不安全,别人可以通过网络端口看到我的数据库)

[root@mariadb ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-networking=1
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

3)重启mariadb,查看网络端口是否还是开启:

[root@mariadb ~]# systemctl restart mariadb.service 
[root@mariadb ~]# netstat -antlupe | grep mysql
[root@mariadb ~]#

4)现在只有通过本地用户可以登录:

[root@mariadb ~]# mysql -uroot -p123456
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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐