Linux之mariadb数据库的安装使用

标题数据库软件的介绍

MariaDB数据库管理系统由MySQL项目创始者重新研发
MariaDB由开源社区进行维护,不受商业专利限制
MariaDB和MySQL在性能上基本保持一致,两者的操作命令也十分相似
相较于MySQL,MariaDB数据库管理系统有了很多新鲜的扩展特性,例如对微秒级别的支持、线程池、子查询优化、进程报告等

安装mariadb软件并设置服务开启等配置

安装mariadb软件
yum install mariadb-server.x86_64 -y
在这里插入图片描述
开启mariadb服务
在这里插入图片描述
查看防火墙的状态
在这里插入图片描述

初始化数据库mysql_secure_installation

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


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
 ... 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

在这里插入图片描述

如果需要让root管理员远程访问数据库,则在初始化操作时设置允许root管理员从远程访问(n),再设置防火墙,使其放行对数据库服务程序的访问请求
数据库服务程序默认会占用3306端口,在防火墙策略中服务名称统一叫作mysql
在这里插入图片描述

添加防火墙策略,或者关闭防火墙

firewall-cmd --permanent --add-service=mysql success
#firewall-cmd --reload success
或者关闭防火墙

登录数据库

格式:
mysql -uroot -p(密码)此时的密码可以不输入,回车后输入密码

在这里插入图片描述另一种登录方式,quit退出
在这里插入图片描述
查看数据库的命令: SHOW databases;
在这里插入图片描述
进入数据库
USE mysql;
在这里插入图片描述
数据库中查看表 SHOW tables;
查找表中的数据:SELECT * FROM user;
在这里插入图片描述
查找表中具体的一项 SELECT Host FROM mysql;
查找表中具体的两项 SELECT Host,User FROM mysql;

where命令的参数及作用
=	相等
<>!=	不相等
>	大于
<	小于
>=	大于或等于
<=	小于或等于
BETWEEN	在某个范围内
LIKE	搜索一个例子
IN	在列中搜索多个值

在这里插入图片描述
查看表格结构 desc user;
在这里插入图片描述
创建数据库:

create database test;    ##创建westos的数据库
use test;                ##使用westos的数据库
create table linux(        ##创建表,使用()表示创建的是一个表
username varchar(50) not null,
password varchar(50) not null,
age varchar(50) );
insert into linux values ('lee','123','20')  ##往表格中插入数据

在这里插入图片描述
在test库中创建表以及查看;
在这里插入图片描述
修改修改表内容

alter table linux rename to zhang;   ##修改表名
alter table zhang add class varchar(50);  ##向表中添加字段
alter table zhang add passwd varchar(50) after age; 
##在age字段之后插入passwd
alter table zhang drop class;  ##删除表中某字段
update zhang set class='linux';  ##修改表中字段的信息
update zhang set class='java' where username='lee';  
##给lee用户添加class为java

在这里插入图片描述
在这里插入图片描述
删除字段
在这里插入图片描述
添加表内容:
在这里插入图片描述

删除数据库
delete from zhang where username=‘lee’ and class=‘Java’;
drop table zhang;
drop database test;
在这里插入图片描述
在这里插入图片描述
更改数据库密码

systemclt stop mariadb  关闭数据库服务
mysqld_safe --skip-grant-tables &
进入数据库更新密码
use mysql
update user set Password=password('redhat') where User='root';
ps aux | grep mysql
kill -9 数据库进程
systemctl restart mariadb

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

管理用户以及授权

grant命令的常见格式以及解释

GRANT 权限 ON 数据库.表单名称 TO 用户名@主机名	对某个特定数据库中的特定表单给予授权
GRANT 权限 ON 数据库.* TO 用户名@主机名	对某个特定数据库中的所有表单给予授权
GRANT 权限 ON . TO 用户名@主机名	对所有数据库及所有表单给予授权
GRANT 权限1,权限2 ON 数据库.* TO 用户名@主机名	对某个数据库中的所有表单给予多个授权
GRANT ALL PRIVILEGES ON . TO 用户名@主机名	对所有数据库及所有表单给予全部授权(需谨慎操作)

在这里插入图片描述
此时,用户lili还没有数据库的任何操作权限,甚至没法查看完整的数据库列表
在这里插入图片描述
以root管理员身份授予用户test查询、更新、删除以及插入等权限
在这里插入图片描述
切换到用户test,查看mysql数据库以及表单user(其余表单因无权限被继续隐藏)
在这里插入图片描述
切回root管理员,移除刚才的授权,再查看用户信息
在这里插入图片描述

发布了57 篇原创文章 · 获赞 0 · 访问量 1344

猜你喜欢

转载自blog.csdn.net/weixin_45674039/article/details/102893523
今日推荐