mysql用户管理
建立一个网站须要链接Mysql,默认是root用户,可是咱们不能老是链接root,万一操做错误就会出问题mysql
对于某个数据库,给user1设定权限 grant all on *.* to 'user1' identified by 'passwd';
mysql> grant all on *.* to 'user' identified by '123'; Query OK, 0 rows affected (0.00 sec)
grant all on *.* to 'user1' @'127.0.0.1' identified by '123456a';
grant:受权 all:全部的权限 *.*:表示全部,第一个*表示库名 @后面也可写成%,表示通配,全部的ip 使用的是socket通讯
此时,咱们退出mysql数据库后再链接mysql,出现报错,由于默认链接使用的是socket,当受权是localhost时,能够直接链接mysql
咱们也能够不用给全部的权限,只给定几个权限 grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.37.1' identified by 'passwd';
grant all on db1.* to 'user3'@'%' identified by 'passwd';
show grants; //查看全部的受权,当前用户的受权
show grants for user2@192.168.37.1; //查看指定用户的受权
经常使用的sql语句
select count(*) from mysql.user; //count(*)表示查找表的行数
select count(*) from mysql.user; //count(*)表示查找表的行数
select * from mysql.db;
select db from mysql.db; //db表示字段
select db,user from mysql.db;
select * from mysql.db where host like '192.168.%'; //模糊匹配
insert into db1.t1 values (1, 'abc'); //name字段abc是字符串,最好加上单引号
update db1.t1 set name='aaa' where id=1;
delete from db1.t1 where id=2; //删除操做
truncate table db1.t1; //只是清空数据
drop table db1.t1; //全部的数据、结构等全清空
drop database db1; //删掉数据库db1
mysql数据库备份
备份库 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
恢复库 mysql -uroot -p123456 mysql < /tmp/mysql.sql
备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql //库与表之间用空格分开
恢复表 mysql -uroot -p123456 mysql < /tmp/user.sql //恢复表只须要写上库名
备份全部库 mysqldump -uroot -p123456 -A >/tmp/123.sql //-A表示全部的库
只备份表结构 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql