linux数据库的基本操作(mysql),linuxmysql
########数据库MysQl########
## Shell中mysql的基本操作
## mysql的安装及运行
## 安装mysql
yum search mariadb
软件包
yum install mariadb mariadb-server -y
软件和client软件
## 启动mariadb服务
systemctl start mariadb
systemctl enable mariadb
## mariadb监听的端口
netstat -antlpe | grep mysql
ss -antlpe | grep mysql
vim /etc/services
## 只允许本地连接,阻断所有来自网络的连接
vim /etc/my.cnf
skip-networking=1
systemctl restart mariadb
## mariadb的初始化
## 设置mysql的登陆密码
mysql_secure_installation
mysql -uroot -p
## 进入mysql的基本使用操作
mysql -uroot -p密码 ## 以root用户身份进入数据库
show databases ## 显示库(类似于目录),里面包含多个表
create database 库名称 ## 创建数据库
use 库名称 ## 进入数据库
drop database 库名称 ## 删除库
show tables ## 显示全部列表
create table linux( ##创建表
name varchar(10) not null,
age int not null);
desc linux ## 显示表的结构
insert into linux values("kobe", "24"); ## 写入内容
insert into linux(age, name) values("23", "james"); ## 按照指定顺序插入内容
update linux set age="81" where name="kobe"; ## 更改表里面的内容
alter table linux add sex varchar(5); ## 添加表的内容
delete from linux where name="james"; ## 删除name所指的内容
select * from linux ## 显示表的全部内容
select name from linux ## 显示表的姓名列
drop table linux ## 删除表
## 用户和访问权限的操作
create user hello@localhost identified by 'hello'; ##创建本机用户 密码为hello
create user hello123@'%' identified by 'hello123' ## 创建用户hello 可以在远程登陆
grant all on linux.* to hello@localhost; ## 给hello@localhost用户授权,如果为all,授权所有权限
flush privileges; ## 刷新,重载授权表
show grants for hello@localhost; ## 查看用户授权
revoke delete,update on linux.* from hello@localhost; ## 删除指定用户授权
drop user hello@localhost; ## 删除用户
## 忘记mysql用户密码时,怎么找回?
1. 关闭mariadb服务以及火墙
systemctl stop mariadb
2. 跳过授权表
mysqld_safe --skip-grant-table &
3. 修改root密码
mysql
> update mysql.user set Password=password('redhat') where User='root';
4. 关闭跳过授权表的进程,启动mariadb服务,使用新密码即可
ps aux | grep mysql
kill -9 pid
mysql -uroot -p
# 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
恢复:
mysqladmin -uroot -pwestos create mariadb2
mysql -uroot -pwestos mariadb2< mariadb.dump