mysql常用操作
查看都有哪些库 show databases;
查看某个库的表 use 库名; show tables;
查看表的字段 desc 表名;
当前是哪个用户 select user();
查询所有用户 select user,host from mysql.user;
当前库 select database();
创建库 create database db1;
创建表 create table tb1 (`id` int(4), `name` char(40));
查看建表语句 show create table tb1\G
插入语句 insert into tb1 value (1,’zhangsan’) ; insert into tb1 (`name`,`id`) values (‘lisi’,2);
insert into tb1 (`id`) values (2);
查看表下的语句;select * from tb1;
查看数据库版本 select version();
查看mysql状态 show status;
修改mysql参数 show variables like ‘max_connect%‘; set global max_connect_errors = 1000;
查看mysql队列 show processlist;
创建普通用户并授权 grant all on *.* to user1 identified by ‘111111’;
grant all on db1.* to ‘user2’@’ip’ identified by ‘111111’;
grant all on db1.* to ‘user3’@’%’ identified by ‘111111’;%代表所有
grant select,update on db1.* to ‘user3’@’%’ identified by ‘111111’
回收用户权限 revoke update on db1.* from ‘user3’@’%‘; revoke all on db1.* from ‘user3’@’%‘;
删除用户 drop user ‘user3’@’%‘;
MySQL查询所有的用户
——-
在mysql里进mysql库
mysql>use mysql;
然后select user表
mysql>select * from user;
或者
select * from mysql.user;
查看某个用户拥有哪些权限:show grants for ‘user’@’ip‘;
更改密码 UPDATE mysql.user SET password=PASSWORD(“newpwd”) WHERE user=’username’ ;
查询 select count(*) from mysql.user; select * from mysql.db; select * from mysql.db where host like ‘10.0.%‘;
清空表 truncate table db1.tb1;
删除表 drop table db1.tb1;
删除数据库 drop database db1;
修复表 repair table tb1 ;
mysql备份与恢复
备份 mysqldump -uroot -p db >1.sql
恢复 mysql -uroot -p db <1.sql
只备份一个表 mysqldump -uroot -p db tb1 > 2.sql
备份时指定字符集 mysqldump -uroot -p –default-character-set=utf8 db >1.sql(字符集视语句的格式而定)
恢复也指定字符集 mysql -uroot -p –default-character-set=utf8 db < 1.sql