基于Linux系统的MariaDB数据库的安装配置

一、Mariadb 简介

数据库是指长期存储在计算机内、有组织的和可共享的数据集合。表是数据库存储数据的基本单位,一个表由若干个字段组成

  • MariaDB 数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用 GPL 授权许可 MariaDB 的目的是完全兼容
    MySQL ,包括 API 和命令行,是 MySQL 的代替品
  • MariaDB 由 MySQL 的创始人 Michael Widenius 主导开发,他早前曾以 10 亿美元的价格,将自己创建的公司
    MySQL AB 卖给了 SUN
  • 随着SUN 被甲骨文收购, MySQL 的所有权也落入 Oracle 的手中
  • MariaDB 名称来自 Michael Widenius 的女儿 Maria 的名字

二、Mariadb的安装和安全初始化

1.安装

[root@localhost ~]# yum install mariadb-server.x86_64 -y
[root@localhost ~]# systemctl start mariadb

这里写图片描述
2.安全初始化
【1】关闭网络接口
数据库的网络接口默认是打开的,安全起见需要关闭接口

[root@localhost ~]# netstat -antlpe| grep mysql   ##查看所开启的网络接口,3306接口是打开的
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN   
27         38251      2921/mysqld    
[root@localhost ~]#vim /etc/my.cnf ##编写配置文件关闭网络接口
10 skip-networking=1
[root@localhost ~]#systemctl restart mariadb
[root@localhost ~]#netstat -antlpe |grep mysql  ##此时没有接口打开

这里写图片描述
具体配置见下图:只增加一行 skip-networking=1,其他内容不变

这里写图片描述
【2】安全初始化

[root@localhost ~]#mysql_secure_installation  ##对其进行安全设定(加密)
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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.
##是否设置root用户密码,输入y并回车或直接回车
Set root password? [Y/n] y   
New password: 
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.
##是否禁止root远程登录,根据自己的需求进行选择,建议禁止!
Disallow root login remotely? [Y/n] 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.
##是否删除test数据库,直接回车
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!
[root@localhost ~]#mysql -uroot -p ##进行登陆数据库

这里写图片描述

三、数据库的基本命令

mysql;                          ##无密码登陆数据库
mysql -uroot -p;                ##root用户登陆数据库
USE mysql;                      ##进入mysql数据库
SHOW DATABASES;                 ##显示databases中所有的库,任何命令必须以“;”结尾   
SHOW TABLES;                    ##显示当前数据库的所有表 
SELECT * FROM user;             ##显示所有字段的数据(字段指列)
SELECT Host,User FROM user;    ##显示user表中Host和User字段的数据
SELECT Host,User FROM user Where Host='localhost';  ##显示user表中Host='localhost'的Host和User字段的数据
DESC user;                      ##显示当前数据库的所有字段,查看user表的数据结构 
quit;                           ##退出数据库

四、数据库的基本使用

1.数据库的查询

[root@localhost ~]# mysql -uroot -p 
MariaDB [(none)]> SHOW DATABASES; ##显示所有的数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> USE mysql ##进入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; ##查看库里有多少表格
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)
MariaDB [mysql]> SELECT User,Host,Password,Select_priv  FROM user; ##查看user表格的User,Host,Password,Select_priv字段
+------+-----------+-------------------------------------------+-------------+
| User | Host      | Password                                  | Select_priv |
+------+-----------+-------------------------------------------+-------------+
| root | localhost | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
| root | 127.0.0.1 | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
| root | ::1       | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
+------+-----------+-------------------------------------------+-------------+
3 rows in set (0.00 sec)
MariaDB [mysql]> DESC user; ##查看user表格的字段(篇幅过长不再演示)
MariaDB [mysql]> SELECT User,Host,Password,Select_priv  FROM user Where User='root';##查看user表格的root用户的User,Host,Password,Select_priv字段
+------+-----------+-------------------------------------------+-------------+
| User | Host      | Password                                  | Select_priv |
+------+-----------+-------------------------------------------+-------------+
| root | localhost | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
| root | 127.0.0.1 | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
| root | ::1       | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
+------+-----------+-------------------------------------------+-------------+
3 rows in set (0.00 sec)
MariaDB [mysql]> SELECT User,Host,Password,Select_priv  FROM user Where User='root' AND Host='localhost';
##查看root用户的localhost主机User,Host,Password,Select_priv字段
+------+-----------+-------------------------------------------+-------------+
| User | Host      | Password                                  | Select_priv |
+------+-----------+-------------------------------------------+-------------+
| root | localhost | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 | Y           |
+------+-----------+-------------------------------------------+-------------+
1 row in set (0.00 sec)

2.数据库的建立,字符插入

[root@localhost ~]#  mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE westos; ##新建一个westos库;
MariaDB [(none)]> SHOW DATABASES;##查看库,已经建立
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE westos ##进入库;
Database changed
MariaDB [westos]> SHOW TABLES;##查看库中的表格;
Empty set (0.00 sec)
MariaDB [westos]> CREATE TABLE linux (      ##新建表格;
    -> username varchar(6)  not null,       ##有效字符长度为6,不能为空;   
    -> password varchar(20) not null);      ##有效字符长度为20,不能为空;   
Query OK, 0 rows affected (0.04 sec)

MariaDB [westos]> SHOW TABLES; ##查看表格,已经建立;
| Tables_in_westos |
+------------------+
| linux            |
+------------------+
1 row in set (0.01 sec)

MariaDB [westos]> DESC linux;##查看表格的字段;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(6)  | NO   |     | NULL    |       |
| password | varchar(20) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> INSERT INTO linux values ('gao','123');##插入信息;
Query OK, 1 row affected (0.05 sec)

MariaDB [westos]> SELECT * FROM linux; ##显示linux表格中的所有信息;
+----------+----------+
| username | password |
+----------+----------+
| gao      | 123      |
+----------+----------+
1 row in set (0.00 sec)

MariaDB [westos]> INSERT INTO linux values ('tom','234');##插入信息;
Query OK, 1 row affected (0.03 sec)

MariaDB [westos]> SELECT * FROM linux;##显示linux表格中的所有信息;
+----------+----------+
| username | password |
+----------+----------+
| gao      | 123      |
| tom      | 234      |
+----------+----------+
2 rows in set (0.00 sec)

3.数据库内容的修改及删除

[root@localhost ~]#  mysql -uroot -p
MariaDB [(none)]> USE westos ##进入库;
Database changed
MariaDB [westos]> ALTER TABLE linux RENAME messages; ##对表格名称的修改;
Query OK, 0 rows affected (0.05 sec)

MariaDB [westos]> SHOW TABLES;
+------------------+
| Tables_in_westos |
+------------------+
| messages         |
+------------------+
1 row in set (0.00 sec)
MariaDB [westos]> ALTER TABLE messages ADD age varchar(4);##在表格中新添age字段;
Query OK, 2 rows affected (0.10 sec)               
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [westos]> SELECT * FROM messages;##显示表格中的所有信息;
+----------+----------+------+
| username | password | age  |
+----------+----------+------+
| gao      | 123      | NULL |
| tom      | 234      | NULL |
+----------+----------+------+
2 rows in set (0.00 sec)
MariaDB [westos]> ALTER TABLE messages DROP age;##删除字段
Query OK, 2 rows affected (0.10 sec)               
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [westos]> SELECT * FROM messages;##显示表格中的所有信息;
+----------+----------+
| username | password |
+----------+----------+
| gao      | 123      |
| tom      | 234      |
+----------+----------+
2 rows in set (0.00 sec)
MariaDB [westos]> ALTER TABLE messages ADD age varchar(4) after username;##在表格中固定位置添加age字段;
Query OK, 2 rows affected (0.16 sec)               
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [westos]> SELECT * FROM messages;##显示表格中的所有信息;
+----------+------+----------+
| username | age  | password |
+----------+------+----------+
| gao      | NULL | 123      |
| tom      | NULL | 234      |
+----------+------+----------+
MariaDB [westos]> UPDATE messages SET password='456' WHERE username='gao';##对表格内容更新;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [westos]> SELECT * FROM messages;##显示表格中的所有信息;
+----------+------+----------+
| username | age  | password |
+----------+------+----------+
| gao      | NULL | 456      |
| tom      | NULL | 234      |
+----------+------+----------+
2 rows in set (0.00 sec)
MariaDB [westos]> DELETE from messages WHERE username='gao';##删除表格内容;
Query OK, 1 row affected (0.02 sec)
MariaDB [westos]> SELECT * FROM messages;##显示表格中的所有信息;
+----------+------+----------+
| username | age  | password |
+----------+------+----------+
| tom      | NULL | 234      |
+----------+------+----------+
1 row in set (0.00 sec)
MariaDB [westos]> DROP TABLE messages;##删除表格;
Query OK, 0 rows affected (0.03 sec)
MariaDB [westos]> DROP DATABASE westos;##删除数据库
Query OK, 0 rows affected (0.00 sec)

五、phpmyadmin数据库图形化管理

下载php apache

[root@localhost ~]# yum install php httpd -y
[root@localhost ~]#  systemctl start httpd

下载phpMyAdmin-3.4.0-all-language 并解压到/var/www/html/

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 ##对安装包解压
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages  phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# rm -rf phpMyAdmin-3.4.0-all-languages.tar.bz2 ##删除安装包
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin ##重命名
[root@localhost html]# cd  mysqladmin/
[root@localhost mysqladmin]# cp config.sample.inc.php  config.inc.php

对config.inc.php进行修改

[root@localhost mysqladmin]# vim Documentation.txt 

这里写图片描述
将上图框内的字符串填入下面文件的引号中

[root@localhost mysqladmin]# vim config.inc.php 

这里写图片描述

下载php-mysq插件

[root@localhost mysqladmin]# yum install php-mysql
[root@localhost mysqladmin]# systemctl restart httpd.service 

查看php支持的服务mysql
这里写图片描述
打开网页测试:http://172.25.254.105/mysqladmin/
这里写图片描述
root密码为在做数据库安全初始化时键入的密码,开始使用吧!

这里写图片描述

六、创建用户和访问权限的修改

1.创建数据库用户

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> CREATE USER gao@'localhost' identified by'123';##创建gao用户,密码123;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SELECT USER FROM mysql.user;##用户添加成功
+------+
| USER |
+------+
| root |
| root |
| gao  |
| root |
+------+
4 rows in set (0.00 sec)

2.用户权限设定
增加用户权限:

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> GRANT SELECT ON westos.* to gao@localhost;##给一个可看的权限;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT UPDATE ON westos.* to gao@localhost;##给一个可以更新的权限;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SHOW GRANTS FOR gao@localhost;##显示gao用户的权限;

这里写图片描述
删除用户权限:

MariaDB [(none)]> REVOKE UPDATE ON westos.* from gao@localhost;
Query OK, 0 rows affected (0.00 sec)

这里写图片描述
3.删除用户

MariaDB [(none)]> DROP USER gao@localhost;
Query OK, 0 rows affected (0.00 sec)

用户gao已经删除:
这里写图片描述

七、数据库忘记root用户密码

[root@localhost ~]# systemctl stop mariadb.service  ##关闭数据库;
[root@localhost ~]# mysqld_safe --skip-grant-tables & ##开启mysql登陆接口并忽略授权表,&为打入后台;
[root@localhost ~]# mysql##此时无需密码即可登录;
MariaDB [(none)]> update mysql.user set Password=password('hello') Where User='root';##重新设定root密码;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
[root@localhost ~]# ps aux |grep mysql ##查看mysql的相关进程;
[root@localhost ~]# kill -9 pid ##结束相关进程;
[root@localhost ~]# systemctl restart mariadb ##重启服务;

重置密码:
这里写图片描述
结束相关进程:
这里写图片描述
重启数据库,即可登录:
这里写图片描述

八、数据库的备份

1.数据库的备份

[root@localhost ~]# mysqldump -uroot -phello nba >/mnt/nba.sql

2.数据库的恢复

[root@localhost ~]# mysql -uroot -phello -e "drop database nba;" ##删除nba库;
[root@localhost ~]# mysql -uroot -phello nba </mnt/nba.sql ##此时没有nba库,无法恢复
ERROR 1049 (42000): Unknown database 'nba'

恢复方法一:
在文件中创建nba库再恢复

[root@localhost ~]# vim /mnt/nba.sql ##对库文件进行修改

这里写图片描述

[root@localhost ~]# mysql -uroot -phello  </mnt/nba.sql   ##恢复数据库
[root@localhost ~]# mysql -uroot -phello -e "select * from nba.player;"   ##恢复成功
+------+-----+
| name | age |
+------+-----+
| kobe | 36  |
+------+-----+

恢复方法二:
用命令创建nba数据库再重新恢复

[root@localhost ~]#mysql -uroot -phello -e "CREATE DATABASE nba;" 
[root@localhost ~]#mysql -uroot -phello nba < /mnt/nba.sql

猜你喜欢

转载自blog.csdn.net/weixin_41476978/article/details/80488534