源地址
https://blog.youkuaiyun.com/xijiao_jiao/article/details/81135736
1.安装mysql
yum search mariadb //查找与mariadb有关的软件包
yum install mariadb mariadb-server -y //安装mariadb的server软件和client软件
- 1
- 2
启动mariadb服务
systemctl start mariadb //启动mariadb服务
systemctl enable mariadb //开机自启mariadb服务
- 1
- 2
mariadb监听的端口
netstat -antlpe | grep mysql
ss -antlpe | grep mysql
vim /etc/services //所有服务与端口默认的对应关系
- 1
- 2
- 3
只允许本地连接,阻断所有来自网络的连接
vim /etc/my.cnf
skip-networking=1
systemctl restart mariadb
- 1
- 2
- 3
2.mariadb的初始化
设置mysql的登陆密码
mysql_secure_installation
mysql -uroot -p
- 1
- 2
mysql基本操作语句
show databases; //显示数据库,类似于目录,里面包含多个表
use mysql; //进入名称为mysql的数据库
show tables; //显示该数据库中的所有表
desc user; //显示表的结构
select * from user; //显示user表的所有内容
select Host,User,Password from user; //显示user表中的某几列内容
create database westos; //创建一个名称为westos的数据库
create table westosuser(username varchar(10) not null,passwd varchar(6) not null); //创建一个westosuser表,表的结构为指定结构
insert into westosuser values ('user1','123453'); //向表中插入内容
insert into westosuser(passwd,username) values("456234","user2"); //按照指定顺序向表中插入数据
update westosuser set passwd='456234' where username="user1"; //更新表中的内容
alter table westosuser add sex varchar(3); //添加sex列到westosuser表中
delete from westosuser where username='user1'; //删除表中用户名为user1的内容
drop table westosuser; //删除表
drop database westos; //删除数据库
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
用户和访问权限的操作
create user hello@localhost identified by 'hello'; //创建用户hello,可以在本机登录,密码为hello
create user hello@'%' identified by 'hello';//创建用户hello,可在远程登录,密码为hello
create database mariadb; 创建一数据库mariadb,对普通用户进行
grant all on mariadb.* to hello@localhost; //给hello@localhost用户授权,如果为all,授权所有权限
(insert,update,delete,select,create)
flush privileges; //刷新,重载授权表
show grants for hello@localhost; //查看用户授权
revoke delete,update on mariadb.* from hello@localhost; //删除指定用户授权
drop user hello@localhost; //删除用户
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.mysql的备份与恢复
备份
mysqldump -uroot -p mariadb >mariadb.dump
mysqldump -uroot -pwestos --no-data mariadb > `date +%Y_%m_%d`_mariadb.dump
mysqldump -uroot -pwestos --all-databases >mariadb4.dump
- 1
- 2
- 3
恢复
mysqladmin -uroot -pwestos create mariadb2
mysql -uroot -pwestos mariadb2< mariadb.dump
- 1
- 2
5.忘记mysql密码时,如何找回密码?
1.关闭mariadb服务
systemctl stop mariadb
- 1
2.跳过授权表
mysql_safe --skip-grant-table &
- 1
3.修改root密码
mysql
> update mysql.user set Password=password('westos') where User='root';
- 1
- 2
4.关闭跳过授权表的进程,启动mariadb服务,使用新密码登录
ps aux | grep mysql
kill -9 pid
mysql -uroot -pwestos
- 1
- 2
- 3
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet">
</div>