什么是mariadb数据库?
把数据保存在一个相对安全的地方,存放各种数据,mysql数据库最为代表,其中表是一种最重要的体现。
mysql的安装
1.下载mariadb的安装包
yum search mariadb #寻找与mariadb相关的软件包
yum install mariadb-server.x86_64 mariadb.x86_64 -y #安装mariadb的服务端可客户端
2.启动mariadb服务
systemctl start mariadb #开启服务
systemctl enable mariadb #设置服务开机自动开启
3.mariadb监听的端口
netstat -antlpe | grep mysql
ss -antlpe | grep mysql
vim /etc/services #所有服务与端口默认的对应关系
4.只允许本地连接,阻断所有来自网络的连接
vim /etc/my.cnf
skip-networking=1 #修改项
systemctl restart mariadb
mysql的操作
1.设置mysql的登陆密码
mysql_secure_installation
登录自己账户即 mysql -uroot -p #超级用户的mysql
2.mysql的基本操作语句
show databases; #显示数据库,类似于目录,里面包含多个表
create database Student; #创建以Student为名的数据库
use Student; #进入名称为Student数据库
show tables; #显示该数据库里的表
create table student_message(name varchar(50) primary key ,py_score int,math_score int,eng_score int,avg_score int,sum_score int) default charset=utf8;
#创建一个表,varchar(50)为字符串类型,长度不超过50。primary key为主键唯一,不可重复。 default charset=utf8为可输入中文并能正确显示。
desc student_message; #显示student_message表的结构
select * from student_message; #显示student_message表中的内容
select py_score,avg_score from student_message; #显示表中的几项
insert into student_message values('westos1',90,80,70,80,240); #向表中插入数据
#按照指定顺序向表中插入数据:
insert into student_message(name,avg_score,sun_score,py_score,math_score,eng_score) values('westos2',100,300,100,100,100);
#更新表中的内容
update student_message set name='bset_one' where avg_score;
#添加sex列到student_message表中
alter table student_message add sex varchar(3)
#删除表中用户名为westos1的记录
delete from student_message where name='westos1';
#删除表student_message:
drop table student_message;
#删除数据库Student:
drop database Student;
## 用户和访问权限的操作
#查看数据库用户信息:
select * from mysql.user;
#创建用户hello,可在本机登陆,密码为hello
create user hello@localhost identified by 'hello';
#创建用户hello,可在远程登陆,密码为hello
create user hello@'%' identified by 'hello';
#创建用户hello,可在172.25.254.250登录,密码为hello
create user [email protected] identified by 'hello';
###这个时候hello用户在mysql里边是没有权限的,需要root 用户给与权限
#查看用户授权
show grants for hello@localhost;
#用root用户创建一个mariadb数据库,
create database mariadb;
grant all on mariadb.* to hello@localhost; #在root用户下给hello@localhost用户授权,如果为all,授权所有权限,如果是*.*就是所有数据库不止mariadb对hello授权所有权限
#(所有权限:insert,delete,create,drop,select,update)
#刷新,重载授权表
flush privileges;
#删除指定用户授权
revoke delete,update on mariadb.* from hello@localhost;
#删除用户
drop user hello@localhost;