Linux使用MariaDB数据库管理系统

使用MariaDB数据库管理系统

1、初始化 MariaDB 服务
安装

安装 mariaDB 数据库主程序及服务端程序:

[root@centos-7-master /]# yum install mariadb mariadb-server -y 
启动

安装完后,启动服务程序,并将其加入到开机启动项中:

[root@centos-7-master /]# systemctl start mariadb
[root@centos-7-master /]# systemctl status mariadb
[root@centos-7-master /]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
初始化

对数据库程序进行初始化操作:

#建议每次安装 MariaDB 或 MySQL 数据库管理系统后都执行这条命令
[root@centos-7-master /]# 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: 	# 输入为root管理员设置的数据库密码123456
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		#禁止 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	#删除 test 数据库并取消对它的访问权限
 - 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):

[root@centos-7-master /]# firewall-cmd --permanent --add-service=mysql
success
[root@centos-7-master /]# firewall-cmd --reload 
success
登陆并查看

登陆 mariadb 并查看当前都有那些数据库:

[root@centos-7-master /]# mysql -u root -p
Enter password: 	#输入之前创建的密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.64-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)
修改root管理员的密码:
MariaDB [(none)]> SET password = PASSWORD('111222');	#修改密码为111222
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> exit
Bye
[root@centos-7-master /]# mysql -u root -p
Enter password: 	#使用新密码111222登陆
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.64-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)]> 
2、管理账户以及授权

使用 root 账户创建新账户 helei:

MariaDB [(none)]> CREATE USER helei@localhost IDENTIFIED BY '123456'; #用户密码123456
Query OK, 0 rows affected (0.00 sec)

查看用户列表:

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> SELECT HOST,USER,PASSWORD FROM user;
+-----------+-------+-------------------------------------------+
| HOST      | USER  | PASSWORD                                  |
+-----------+-------+-------------------------------------------+
| localhost | root  | *890406EBC2A6D76621F6015C608E2B270BC92D22 |
| 127.0.0.1 | root  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ::1       | root  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | helei | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------+-------------------------------------------+
4 rows in set (0.00 sec)

切换到 helei 账户,会发现 helei 没有相应的数据库操作权限:

MariaDB [mysql]> exit
Bye
[root@centos-7-master /]# mysql -u helei -p
Enter password: 	#输入密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.64-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;	#helei用户无权限查看完整的数据库列表
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
GRANT 命令授权
GRANT 命令的常见格式以及解释
命令											   作用
GRANT 权限 ON 数据库.表单名称 TO 账户名@主机名		   对某个特定数据库中的特定表单给予授权
GRANT 权限 ON 数据库.* TO 账户名@主机名			  	对某个特定数据库中的所有表单给予授权
GRANT 权限 ON *.* TO 账户名@主机名				  	 对所有数据库及所有表单给予授权
GRANT 权限1,权限2 ON 数据库.* TO 账户名@主机名 		对某个数据库中的所有表单给予多个授权
GRANT ALL PRIVILEGES ON *.* TO 账户名@主机名 		对所有数据库及所有表单给予全部授权(需谨慎操作)

以root账户对helei账户进行授权:

MariaDB [(none)]> exit
Bye
[root@centos-7-master /]# mysql -u root -p
Enter password: 	#输入root的密码111222
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.64-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)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> GRANT SELECT,UPDATE,INSERT,DELETE ON mysql.user TO helei@localhost;
Query OK, 0 rows affected (0.00 sec)

再查看一下账户helei的权限:

MariaDB [mysql]> SHOW GRANTS FOR helei@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for helei@localhost                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'helei'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'helei'@'localhost'                                |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

切换到helei账户:

MariaDB [mysql]> exit
Bye
[root@centos-7-master /]# mysql -u helei -p
Enter password: 	#helei密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.64-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;	#现在有权限可以看到mysql数据库了,但无权限查看所有数据库列表
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> SHOW TABLES;	#可以操作user表了,但其它还有20多张表因为没有权限被隐藏
+-----------------+
| Tables_in_mysql |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)
REVOKE 命令移除授权

现在用 root 账户用 revoke 命令移除刚才对 helei 的授权:

MariaDB [mysql]> exit
Bye
[root@centos-7-master /]# mysql -u root -p
Enter password: 	#root密码111222
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.64-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)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> REVOKE SELECT,INSERT,UPDATE,DELETE ON mysql.user FROM helei@localhost;
Query OK, 0 rows affected (0.00 sec)

再来查看一下 helei 账户的权限信息,helei 的权限已被移除了:

MariaDB [mysql]> SHOW GRANTS FOR helei@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for helei@localhost                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'helei'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3、创建数据库与表单
常用的创建数据库的命令以及作用:
命令															作用
CREATE DATABASE 数据库名称 									  创建新的数据库
DESCRIBE 表单名称 											   描述表单
UPDATE 表单名称 SET attribute=新值 WHERE attribute>原始值 	    更新表单中的数据,加where子句
USE 数据库名称 												  指定使用的数据库
SHOW databases 												 显示当前已有的数据库
SHOW tables 												 显示当前数据库中的表单
SELECT * FROM 表单名称 										   从表单中选中所有记录值
DELETE FROM 表单名 WHERE attribute=值 						   从表单中删除某个记录值
4、数据库的备份及恢复
mysqldump 命令备份

mysqldump 命令用于备份数据库数据,格式为 “mysqldump [参数] [数据库名称]”:

现在将已创建的 heleidb 数据库中的内容导出成一个文件,并使用输出重定向符保存到本机 /home/helei/Downloads 目录下:

[root@centos-7-master /]# mysqldump -u root -p heleidb > /home/helei/Downloads/heleidb.dump
Enter password: 	#输入数据库root账户密码111222
[root@centos-7-master /]# ll -lh /home/helei/Downloads/
total 4.0K
-rw-r--r-- 1 root root 1.9K Feb 18 12:09 heleidb.dump

然后进入 MariaDB 数据库管理系统,彻底删除 heleidb 数据库,这样 mybook 数据表单也将被彻底删除。然后重新建立 heleidb 数据库:

[root@centos-7-master /]# mysql -u root -p
Enter password: 	#root密码111222
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.64-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;	#heleidb数据库还在
+--------------------+
| Database           |
+--------------------+
| information_schema |
| heleidb            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> DROP DATABASE heleidb;	#删除heleidb
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> CREATE DATABASE heleidb;	#新建heleidb
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| heleidb            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
数据恢复

接下来进行数据恢复,使用输入重定向符把刚刚备份的数据库文件导入到 mysql 命令中,然后执行该命令。接下来登录到 MariaDB 数据库,就又能看到 heleidb 数据库以及 mybook 数据表了:

[root@centos-7-master /]# mysql -u root -p heleidb < /home/helei/Downloads/heleidb.dump
Enter password: 	#root密码123456
[root@centos-7-master /]# mysql -u root -p 
Enter password: 	#root密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 5.5.64-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 |
| heleidb            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use heleidb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [heleidb]> SHOW tables;
+-------------------+
| Tables_in_heleidb |
+-------------------+
| mybook            |
+-------------------+
1 row in set (0.00 sec)

MariaDB [heleidb]> DESCRIBE mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| price | int(11)  | YES  |     | NULL    |       |
| pages | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MariaDB [heleidb]> SELECT * FROM mybook;	#mybook数据表中的数据也全部恢复
+------+-------+-------+
| name | price | pages |
+------+-------+-------+
| aaa  |    22 |   333 |
| bbb  |    44 |   333 |
+------+-------+-------+
2 rows in set (0.00 sec)

你可能感兴趣的:(程序人生,mariadb,mysql,linux,centos,运维)