安装mariadb数据库
使用yum install mariadb*
进行安装,遇到输入的地方就按Y;安装完成后可以开启数据库;mysql默认端口号3306
如果端口被占用输入rm -f /var/run/yum.pid
# 临时启动 mariadb
systemctl start mariadb
# 临时关闭 mariadb
systemctl stop mariadb
# 重启 mariadb
systemctl restart mariadb
# 设置开机自动启动
systemctl enable mariadb
# 关闭开机自动启动
systemctl disable mariadb
# 重新加载服务配置文件
systemctl reload mariadb
# 查看 mariadb 状态
systemctl status mariadb
# 远程连接 mysql
mysql -h10.9.63.168 -uroot -p
登录mariadb数据库
使用mysql -uroot -p
,刚安装好的没有密码,直接回车就行;
mysql 基本命令
查
# 查看数据库
show databases;
# 使用数据库
use 数据库名;
# 查看有哪些表
show tables;
# 查看表结构
desc 表名;
describe 表名;
# 查看创建数据库过程
show create database 数据库名;
# 查看创建表过程
show create table 表名;
# 查询表数据内容
select * from 表名;
# 查询表对应关键字数据
select 字段名,字段名,字段名 from 表名;
# 按条件查询表
select * from 表名 where 字段名='值';
# 查询表内相似的值
select * from 表名 where name like '值%';
# 查询表值升序、降序
select * from 表名 order by 字段名 asc;
select * from 表名 order by 字段名 desc;
# 查询表 代表查看内容从m条开始,只查看n条
select * from 表名 limit m,n;
改
# 修改表中对应关键字数据
update user set password=password("123456") where user="root";
update user set host="10.9.63.128" where user="root";
update 表名 set 字段名="10.9.63.128" where 字段名="root";
# 修改表名
rename table 原表名 to 新表名;
# 修改Student表sno关键字改为Sno
alter table Student change sno Sno char(5) not null;
增
# 创建数据库 数据库名 以utf-8编码
create database 数据库名 character set utf8;
# 创建表
create table 表名(字段1 类型(宽度) 约束条件,字段2 类型(宽度) 约束条件;
# 往表内插入数据
insert into 表名(字段1,字段2) values('值1','值2');
insert into 表名 values('值1','值2'),('值1','值2');
insert into 表名 set 字段1='值',字段2='值';
删
# 删除数据库
drop database 数据库名;
# 删除表
drop table 表名;
# 删除表内字段值
delete from 表名 where 字段='值';
其他
# 更新数据库
flush privileges;
# 开启远程连接数据库
grant all privileges on *.* to 'root'@'%' identified by '654321' with grant option
# 授权用户开启远程连接数据库
grant all on bbs.* to 'lmx'@'192.168.1.128' identified by '123456';
# 撤销授权用户开启远程连接数据库
revoke all on bbs.* from 'lmx'@'192.168.1.128';
# 退出数据库
exit
创建表练习
# 创建学生表 Student
# 学号 Sno,设置主键,不允许为空
# 学生姓名Sname
# 学生性别Ssex
# 学生出生年月日Sbirthday
# 学生所在班级 Classsh