grant all privileges on *.* to 'root'@'%'> 二、mysql数据库基本操作
--------------------------库----------------------------
//创建数据库 create database Myschool;
//查看数据库 show databases;
//删除数据库 drop database Myschool;
//进入数据库 use Myschool;
---------------------------表---------------------------
//创建表 create table info (id int not null primary key auto_increment,name char(10) not null, score decimal(5,2),hobby int(2));
//删除表 drop table info;
//查看表结构 desc info;
//查看Mycshool中的表 show tables;
//修改数据表名> //多表查询 select * from info inner join hob where info.hobby=hob.id;
select info.name,ifo.score.hob.hobname from info inner join hob where info.hobby=hob.id;
//别名查询 select i.name,i.score.h.hobname from info i inner join hob h where i.hobby=h.id;
//查询生成新表 create table info2 select i.name,i.score.h.hobname from info i inner join hob h where i.hobby=h.id;
----------------------数据---------------------------
//查看info表中的数据 select * from info;
//插入数据 insert into info (id,name,score) values (5,'tianqi',80);
//筛选 条件 select * from info where>
//修改更新信息 update info set score=75 where> //删除信息 delete from info where name='test';
//添加字段>
//修改列名>
//修改字段的数据类型> //排序(升序/降序)select * from info where 1=1 order by score; 默认升序
select * from info where 1=1 order by score asc; 升序
select * from info where 1=1 order by score desc; 降序
//分组 select * from info group by hobby
//聚合函数
统计 count() 例:select count(*) from info2;
平均值 avg() 例:select avg(score)from info2; 三、mysql索引与事务
索引创建方法:create index 索引名字 on 列的列表
例:create index> 查询索引:show index from info; show index from info \G;
添加字段:> 删除字段:alter table info drop column age;
创建全文索引:create table info(descript TEXT,FULLTEXT(descript)); engine=MyISAM
多列索引:create index multi_index on info(name,adress);
事务:一组操作共同执行或者都不执行,结果保持一致;
begin 开始 set autocommit=0 :禁止自动提交
commit 提交
rollback 回滚
savepoint s1; 定义回滚点
rollback to savepoint s1; 回滚到定义的回滚点
事务四个特性:
原子性,一致性,隔离性,持久性
视图 数据库中的虚拟表
作用: 一张表或者多表中的数据给不同的权限用户提供访问
create view score_view as select * from info where score > 80; 四、用户管理
创建
create user ‘username’@‘host’>
grant 权限 on 数据库.表 to 用户@主机> 查看
select user,authentication_string,host from user;
删除
drop user 'lisi'@'localhost';
重命名
rename user 'zhangsan'@'localhost' to 'test'@'192.168.218.130';
密码明文转化成密文
select password('abc123');
create user ‘username’@‘host’> 更改用户密码
set password for 'test'@'192.168.218.130' = password('abc123');
忘记密码(5.7)
systemctl stop mysqld
vim /etc/my.cnf
[mysqld]
skip-grant-tables
systemctl start mysqld
mysql
update mysql.user set authentication_string = password('abc123') where user = 'root';
赋权
grant 权限 on 数据库.表 to 用户@主机> 撤销权限
revoke 权限 on 数据库.表 from 用户@主机;
查看权限
show grants for 用户@主机;
日志管理
vim /etc/my.cnf
[mysqld]
log-error=/usr/local/mysql/data/mysql_error.log #错误日志
general_log=ON #通用日志
general_log_file=/usr/local/mysql/data/mysql_general.log
log_bin=mysql-bin #二进制日志(记录所有操作)
查看二进制文件
mysqlbinlog --no-defaults mysql-bin.00001
慢日志
slow_query_log=ON
slow_query_log_file=mysql-slow_query.log
long_query_time=1 #参照时间