第11章 MariaDB数据库2020-06-17

 MariaDB数据库

yum groupinstall MariaDB    安装MariaDB 

systemctl enable mariadb.service     开机自启

systemctl restart mariadb.service     启动一下

netstat -antulp | grep mysql    看一下端口

firewall-cmd --permanent --add-service=mysql     防火墙设置一下

firewall-cmd --reload     重新读配置

vim /etc/my.cnf    MariaDB的配置文件 官方默认

 MariaDB整了四套配置文件

ll /usr/share/mysql/    四套配置文件在这里


根据数据规模选择配置文件

ll /var/lib/mysql/    存放数据文件,一个文件夹就是一个数据库

刚装完默认没有密码,直接mysql就进入MariaDB界面

show databases;    查看有几个数据库


use mysql    使用mysql数据库

show tables;    查看有几张表

desc user;    查看user表的结构

select database();    查看当前所在数据库

select user();     查看当前身份

show variables like 'innodb%';    查看环境变量

ctrl + d 退出MariaDB

mysqladmin -u root password '123456'    给root设置登陆MariaDB的密码

mysql -u root -p    设置密码后就这样子登陆

MariaDB [(none)]> status    查看数据库信息

出去locale 查看当前语言环境

MariaDB [(none)]> system ls -l /    调用系统命令

MariaDB [(none)]> source /scott.sql    调用脚本建库https://my.oschina.net/iamhere/blog/357809?p={{currentPage-1}}

MariaDB [scott]> show create database scott;    查看数据库语言环境

MariaDB [scott]>show create table emp;    查看表的语言环境

alter database scott charset utf8;    修改数据库语言环境

alter table emp charset utf8;    修改表的语言环境

这样子改太麻烦 vim /etc/my.cnf直接去配置文件定义


systemctl restart mariadb.service    重启服务就语言环境就改过来了

但是改了配置文件以后是以后建立的才有效,之前的不行

drop database scott;    删除scott数据库,然后再建立一次就好了哈哈

/////////////////////////

select * from xxx;   显示全表

select ename,sal  from xxx;    就显示 xxx表中的ename,sal

select ename,sal+10000 from xxx;    给每条sal加10000

select ename,sal+10000 as newsal from xxx;     给,sal起个别名

select ename,sal+10000  newsal from xxx ;    as不写也可以

select ename,(sal+200)*3 from xxx ;    支持括号

select ename,sal+ifnull(comm,0)  as zhonshouru from xxx;     如果ifnull()里面是空值就改成0(null空值加减乘除会变null)

select distinct deptno from tmp;        合并重复行

select * from xxx where deptno=30 ;   加条件

select * from xxx where sal>3000 ;   加判断

select * from xxx where ename like ‘%TT%’;   模糊查找(字符和日期要加单引号)

select * from xxx where ename like ‘_TT%’;    下划线明确前面只有一个字符,一个_等于一个字符

select * from emp where deptno=30 and sal>2000;    与   支持与或非

select * from emp where deptno=30 or sal>2000;   或  支持与或非

select * from emp where not sal>2000;    非    支持与或非

select * from emp order by sal;    排序,默认升序,从小到大

select * from emp order by sal desc;    降序

select * from emp order by deptno,sal;    先按deptno排序,再按sal排序

select concat(ename,sal,comm) from emp;    连接ename,sal,comm结果集

select concat(ename,sal,ifnull(comm,0)) from emp;    函数嵌套

select concat(ename,'\'s sal is ',sal) from emp;    连接字符串,引号需要转义符 \

select count(*) from emp;    统计几行

select sum(sal),min(sal),max(sal),avg(sal) from emp;   总和, 最小,最大,平均值,多行函数

select deptno,sum(sal),min(sal),max(sal),avg(sal) from emp group by deptno;    分类group by 


select ename,dname from emp,dept where emp.deptno=dept.deptno;    多表查询

select y.ename,j.ename from emp y,emp j where y.mgr=j.empno;    同一张表多表查询,起了别名y和j

select ename,sal,grade from emp,salgrade where sal between losal and hisal; 多表查询,sal在losal和 hisal之间对应列出

select ename,dname,sal,grade from emp,dept,salgrade where emp.deptno=dept.deptno and emp.sal between losal and hisal;三表查询

create database qindatabase;    创建数据库

create table qintable(id int(2),name varchar(10),mail varchar(20));    建表


查看表结构

insert into qintable(id,name,mail) values(1,'qin1','qin@qin.com');    插入数据

insert into qintable values(2,'qin2','qin2@qin.com');    每一列都插前面可以不写

insert into qintable(id,name) values(3,'qin3');    插入个别列

insert into qintable(id,mail) values(4,'qin4@qin.com');      插入个别列

insert into qintable values(5,'qin5','qin5@qin.com'),(6,'qin6','qin6@qin.com'),(7,'qin7','qin7@qin.com');    一次插入多行数据

delete from qintable where id=4;    删除第4行

update qintable set name='bing' where id=2;    把id等2的name改成bing

create table qin1 like qintable;    采用qintable的表结构建立qin1表

insert into qin1 select * from qintable;    把 qintable表的数据插入到qin1表

create table qin2 as select * from qintable;    直接创建一个结构数据跟qintable都一样的表qin2

create table qin3 as select * from qintable where 0=1;    采用qintable的表结构建立qin1表,where 0=1不成立所以没数据

alter table qintable add newlist varchar(20);    给表qintable添加一列叫newlist

alter table qintable drop newlist;    删除newlist这列

alter table qintable add firstlist varchar(20) first;    在第一列(first)添加一列叫firstlist 


alter table qintable add afterid varchar(20) after id;    在id列后面(after id)添加一列afterid

delete from qin1;    删除qin1表的所有内容

drop table qin1;    直接删除qin1表

truncate qin3;    删除qin3表的所有内容  ,真删,磁盘上也没了

drop database qindatabase;    删除数据库

//////////////外部数据导入

vim /qin.txt    建数据

MariaDB [(none)]> create database qindatabase;    从新建库

MariaDB [(none)]> use qindatabase;    进入

create table qintable(id int(4),name varchar(10),email varchar(20));    建表

load data infile '/qin.txt' into table qintable fields terminated by ',' lines terminated by '\n';    将qin.txt的数据导入表qintable,后面的fields terminated是数据的分隔符是逗号,lines terminated 结束符是回车\n

updata qintable set name='bing' where id=2;    还能修改内容,但是外面qin.txt的数据不变

select * from qintable into outfile '/var/lib/mysql/qindatabase/qintable.txt' fields terminated by ',' lines terminated by '\n';    将qintable表的数据导出到'/var/lib/mysql/qindatabase/下的qintable.txt    

导出位置注意权限问题,还要注意SElinux上下文

 mkdir /backup

semanage fcontext -a -t mysqld_db_t '/backup{.*}?'    修改上下文

restorecon -RFv /backup/    还得还原一下才生效

////////////////备份还原

MariaDB [(none)]>use scott;    进入scott数据库

退出MariaDB 操作

mysqldump -u root -p scott > /scott.dump    使用root身份将数据库scott备份到/scott.dump   ,需要输入root密码

mysqldump -u root -p scott emp > /scott.emp.dump    将数据库scott的emp表备份到/scott.emp.dump

mysqldump -u root -p scott dept salgrade > /scott.dept+salgrade.dump     同时备份dept,salgrade表,多表备份

进入MariaDB 操作

MariaDB [(none)]> drop database scott;    删了scott数据库

MariaDB [(none)]> create database qin;    建立空数据库qin

退出MariaDB 操作

mysql -u root -p qin < /scott.dump     使用root身份还原数据库qin,数据来源/scott.dump

进入MariaDB 查看发现数据回来了

////////////////单表还原

MariaDB [qin]> drop database qin;    删了qin数据库

MariaDB [(none)]> create database qin;     建立空数据库qin

退出MariaDB 操作

mysql -u root -p qin < /scott.emp.dump     单表导入

mysql -u root -p qin < /scott.dept+salgrade.dump

进入查看数据回来了

////////////////////MariaDB用户权限

user mysql    进入mysql数据库

show tables;    其中user表存放MariaDB的用户


MariaDB [mysql]> create user qin@'%' identified by '123456';     创建用户qin,登陆位置所有ip都可以连接过来,密码123456,

去登陆发现不行,因为它需要所有网络登陆才能进入

去2号机安装MariaDB

yum install mariadb

mysql -h 192.168.100.1 -u qin -p    通过远程登陆192.168.100.1使用qin账户就可以登陆了

MariaDB [(none)]> create user qin@'localhost' identified by '123456';    再回主机创建一个localhost用户,这个用户就可以直接登陆


MariaDB [mysql]> drop user qin@'%';    把这qin百分百用户给删了

MariaDB [mysql]> create user qin@'192.168.100.2' identified by '123456';    创建了一个可以通过192.168.100.2登陆的用户

MariaDB [mysql]> create user qin@'192.168.100.0/255.255.255.0' identified by '123456';    创建网段用户

MariaDB [(none)]> show privileges;    查看权限

MariaDB [(none)]>grant select on scott.* to qin@'localhost';    将对scott数据库下的所有表的查询权限(select)授权给 qin@'localhost'用户

MariaDB [(none)]> flush privileges;    如果权限没有生效,刷新权限表

再次使用qin登陆MariaDB 就可以看到scott数据库了,查询表,但是不能删改

MariaDB [(none)]> show grants;    查看自己的权限

MariaDB [(none)]> show grants forqqin@'localhost';    查看qin@'localhost'的权限

MariaDB [(none)]> revoke select on scott.* from qin@'localhost';    收回qin@'localhost对 scott.*的select权限

MariaDB [(none)]> grant all on *.* to qinbing@'localhost' identified by '123456'    创建用户qinbing@'localhost'并且授权全部(all)权限对所有数据库( *.* );

MariaDB [(none)]> grant all on *.* to qinbing@'%' identified by '123456';    增加远程登陆

退出MariaDB

mysqladmin -u root -p password 'redhat';    修改登陆密码为redhat

MariaDB [mysql]> update user set password='123456' where user='root' and host='localhost';    在数据库里改密码,将root(localhost)的密码改为123456    但是发现密码是明文的

update user set password=password('123456') where user='root' and host='localhost';    增加password('123456')变密文

MariaDB [mysql]> set password=password('redhat');    直接更改当前用户密码为redhat

MariaDB [mysql]> set password for qin@'localhost'=password('redhat');    对别的用户改密码

///////////////破解MariaDB密码

systemctl stop mariadb.service     先关了MariaDB

vim /etc/my.cnf    修改配置文件

跳过权限表

systemctl restart mariadb.service     启动数据库

mysql -u root -p    直接登陆,不用输入密码回车就进入数据库了

MariaDB [(none)]> use mysql    进入mysql 

MariaDB [mysql]> select host,user,password from user;

MariaDB [mysql]> update user set password=password('123456') where user='root';    把密码改了就好了

然后退出去把配置文件里的skip-grant-tables给删了

systemctl restart mariadb.service     重启数据库

///////////////破解MariaDB密码 2

systemctl stop mariadb.service     先关了MariaDB

mysqld_safe --skip-grant-tables    输入这个跳过权限,然后打开另一个窗口

直接mysql就进入数据库了,然后就是同上操作改掉密码就好了

systemctl restart mariadb.service     最后重启数据库

////////////////////

mysql_secure_installation    提高安全性安全安装(用于生产环境设置)

systemctl restart mariadb.service     重启数据库

vim /etc/my.cnf    修改配置文件

禁止网络用户登陆


没禁止网络用户之前

重启机器,systemctl reboot

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,699评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,124评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,127评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,342评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,356评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,057评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,654评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,572评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,205评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,343评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,015评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,704评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,196评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,690评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,348评论 2 358