MySQL常用命令
2016-06-08  本文已影响56人 
dotdiw
用户管理命令
- 
新增用户
- 
create user 命令
CREATE USER username IDENTIFIED BY 'password'; - 
grant授权命令
GRANT ALL PRIVILEGES ON *.* to mysqluser@'%' IDENTIFIED BY 'passworkd' WITH GRANT OPTION; - 
insert在user表中插入新用户
INSERT INTO user (host,user,password) VALUES ('%','test3',password('123')); 
 - 
 - 
删除用户
- 
drop删除命令
DROP user username; - 
delete删除用户
DELETE user FROM user where user='test2'; 
 - 
 - 
用户查看命令
SELECT user,host,super_priv from user; 
grant语句
- 
全局级(可以为某一个用户分配全部的权限,存储在mysql.user中)
grant all privileges on *.* to mysqluser@'%';注:mysqluser拥有全部的管理权限,但是没有分配权限,也就是不能创建新用户
grant all privileges on *.* to mysqluer@'%' with grant option;注:mysqluser用户拥有创建新用户的权限
 - 
数据库级(给定一个数据库中所有的目标操作权限,存储在mysql.db和mysql.host中)
grant all privileges on database.* to mysqluser@'%' identified by 'password' with grant option;注:给用户mysqluser在database上的权限,不能作用于其他数据库
 - 
数据库表级(给用户一个数据表中的操作权限,存储在mysql.tabls_priv中,通常select insert delete update等)
grant select on database.table1 to mysqluser@'%' indentified by 'password' with grant option; - 
字段级(用户只对某一字段拥有某些权限,存储在mysql.columns_priv)
grant insert(columnname) on database.tabel to mysqluser@'%' indentified by 'password' with grant option; - 
子程序级(用户用于存储过程,修改过程,执行存储过程,或函数等操作采用子程序级管理,存储在mysql.procs_priv中)
- 
如用户mysqluser获得数据库database中的test_pro 的执行权限
grant execute on procedure database.test_pro to mysqluser@'%'; - 
用户mysqluser执行database中的test_func函数、
grant execute on funcation database.test_func to mysqlur@'%'; 
 - 
 
show grants 语句
- 
查看自己的操作权限
mysql> show grants; - 
查看其他用户的操作权限
mysql> show grants for test3; 
revoke语句(权限收回)
- 
消除字段集
revoke insert(字段名)no database.tabel from mysqluser; - 
消除表级某一类权限
revoke insert,update,delete on mysql.user form test3; - 
撤销全局级
revoke all privileges,grant option from test3; 
导入导出命令(简单介绍)
- 
导入命令
- 
mysqlimport (需要首先创建对应的表格)
mysqlimport [-d/f...] -u root -p table data_import.txt参数:
-d 在新数据导入数据表之前删除表中原有数据 -f 忽略执行中的错误,强行插入数据 -i mysqlimport 跳过或者忽略有相同唯一关键字的记录 -r 替换表中有相同唯一关键字的记录 --fields-enclosed- by=char 指定文本中的数据记录是以什么括起来的,默认没有被字符括起 --fields-terminated- by=char 指定各数据值之间的分隔符,用户可以用此选项指定,默认为Tab --lines-terminated- by=str 指定文本中行与行之间的的分隔符或字符串,默认为newline,用户可选择用一个字符串来替代一个单词的字符 --lock-tables 数据被插入之前锁住数据表 - 
source导入命令
source data.sql - 
load(专职导入数据)
load data infile 'data-import.txt' into tabel data_improt; - 
restore恢复
restore table tablename from '/home/backup';(此为路径) 
 - 
 - 
导出命令
- 
mysqldump
mysqldump [-r/...] databasenaem > data.sql;参数:
--add-locks 枷锁,该选项是在insert语句中加入lock table和unlock table --add-drop-table 在每一个表前加入DROP TABEL IF EXISTS语句 -f 或 -force 忽略错误强行导出 -d 或 -no-data 导出时不创建insert语句 -t 或 -no-create-info 在数据导出前不创建create table 语句 -F 或 -flush-logs 在执行导出前刷新服务器的log --delayed-insert 在insert命令中加入delay选项 -c 或 -complete_insert 对每一个insert语句加上列(filed)的名字 -l 或 -lock-tables 数据被导出时锁住表 - 
命令导出
select * from tablename into outfile 'filename.txt'; - 
backup语句
backup table tablename to '/home/backup';(此为路径) 
 - 
 
复制数据库
- 
表与数据的复制
create table tablename1 select * from tablename2; - 
表结构复制
- 
利用create&select
create table tabelname1 select * from tablename2 where <条件>; - 
利用like语句
create tabel 目标表 like 源数据表; 
 - 
 - 
记录复制
- 
全表记录复制
insert into 目标表名 select * from 源数据表名; - 
部分记录复制
insert into 目标表名 select * from 源数据表名 where <条件>; - 
部分字段复制(前提是先创建一张表)
insert into 目标表名(字段) select (字段) from 源数据表名; 
 - 
 
may you success !