数据库mariadb的基本操作

####数据库的安装及初始安全配置
1.安装及配置
yum install mariadb-server -y  ##安装数据库服务软件

systemctl start mariadb        ##开启数据库
mysql                ##进入数据库
netstat -antlpe |grep mysql    ##数据库的网络端口

vim /etc/my.cnf            ##禁止网络登陆
skip-networking=1        ##跳过网络服务

systemctl restart mariadb    ##重启数据库

mysql_secure_installation    ##数据库初始安全配置
mysql -uroot -p            ##进入数据库,-u是登陆用户,-p该用户密码

######数据库的基本sql语句
2.查询
show databases;        ##显示数据库
use mysql;        ##进入mysql数据库
show tables;        ##显示当前库中表的名称
select * from user;    ##查询user表中的所有内容
desc user;        ##查询user表的结构

3.数据库以及表的建立
create database westos;            ##创建westos数据库
create table linux(            ##创建表linux
username varchar(15) not null,        ##username字段,字符最大长度为15,且不为空
password varchar(15) not null
);
insert into linux values ('user1','passwd1');    ##给linux表中添加数据
insert into linux values ('user1',password('passwd1'));    ##给linux表中添加数据,且密码加密

4.更新数据库信息
update linux set password=password('passwd2') where username='user1';    ##更新名字是user1的密码
update linux set password=password('123') where (username='user1' or username='user2');    ##更新user1,user2密码

delete from linux where username='user1';        ##删除用户user1

alter table linux add age varchar(4);        ##在末尾增加age字段
alter table linux add age varchar(5) after name;    ##在name字段后增加age字段
alter table linux drop age;            ##删除linux表中的age字段

5.删除数据库
delete from linux where username='user1';    ##从linux表中删除user1用户
drop table linux;                ##删除linux表
drop database westos;                ##删除westos数据库
6.数据库的备份
mysqldump -u root -pwestos --all-database    ##备份所有表中的所有数据
mysqldump -u root -pwestos --all-database --no-date    ##备份所有表,但不备份数据
mysqldump -u root -pwestos westos         ##备份westos数据库
mysqldump -u root -pwestos westos > /mnt/westos.sql    ##备份westos库,并将数据保存到westos.sql文件中
mysqldump -u root -pwestos westos linux > /mnt/linux.sql    ##备份westos数据库中的linux表
mysql -uroot -pwestos -e "create database westos;"        ##还原数据时,应先建立相应的westos库
mysql -uroot -pwestos westos < /mnt/westos.sql            ##将数据导入westos库

7.用户授权
create user hello@localhost identified by 'hello';        ##建立用户hello,此用户只能通过本机登陆
create user hello@'%' identified by 'hello';            ##建立用户hello,此用户可以通过网络登陆
grant insert,update,delete,select on westos.linux to hello@localhost;    ##用户授权
grant select on westos.* to hello@'%';                
show grants for hello@'%';                    ##查看用户授权
show grants for hello@localhost;
revoke delete on westos.linux from hello@localhost;        ##去除用户授权权力
drop user hello@'%';                        ##删除用户

8.密码修改
mysqladmin -uroot -pwestos passwd hello                ##已知超级用户密码,直接修改密码
#########当超级用户忘记密码####
systemctl stop mariadb                        ##关闭mysql

mysqld_safe --skip-grant-tables &                ##开启mysql登陆接口并忽略授权表

mysql                                ##直接登陆,不需密码
update mysql.user set Password=password('hello') where User='root'    ##更新超级用户密码
ps aux |grep mysql                        ##过滤所有mysql的进程,并结束这些进程
killall -9 mysql
kill -9 mysqlpid
systemctl start mariadb                        ##重新开启mysql
mysql -uroot -phello                        ##登陆测试

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值