用户授权在mysql中使用grant命令就可以了,我相信各位都会有了解过了,下面小编为各位介绍一个Mysql下建立用户授权权限例子,希望本文章对各位有帮助.
建立用户,授权数据库:
- mysql>createuser'byrd'@'localhost'identifiedby'admin123';#建立主机为localhost,密码为admin123的用户byrd
- QueryOK,0rowsaffected(0.05sec)
- mysql>showgrantsfor'byrd'@'localhost';#查看byrd权限,USAGE表示连接权限
- +-------------------------------------------------------------------------------------------------------------+
- |Grantsforbyrd@localhost|
- +-------------------------------------------------------------------------------------------------------------+
- |GRANTUSAGEON*.*TO'byrd'@'localhost'IDENTIFIEDBYPASSWORD'*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'|
- +-------------------------------------------------------------------------------------------------------------+
- 1rowinset(0.00sec)
- mysql>selectuser,hostfrommysql.user;
- +------+-----------+
- |user|host|
- +------+-----------+
- |root|127.0.0.1|
- |root|::1|
- |root|lamp|
- |byrd|localhost|
- |root|localhost|
- +------+-----------+
- 5rowsinset(0.00sec)
- mysql>grantallongbk.*to'byrd'@'localhost';#用户byrd、主机localhost对数据库gbk拥有所有权限
- QueryOK,0rowsaffected(0.01sec)
- mysql>showmysqlsfor'byrd'@'localhost';
- +-------------------------------------------------------------------------------------------------------------+
- |Grantsforbyrd@localhost|
- +-------------------------------------------------------------------------------------------------------------+
- |GRANTUSAGEON*.*TO'byrd'@'localhost'IDENTIFIEDBYPASSWORD'*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'|
- |GRANTALLPRIVILEGESON`gbk`.*TO'byrd'@'localhost'|
- +-------------------------------------------------------------------------------------------------------------+
- 2rowsinset(0.00sec)
- mysql>flushprivileges;
- mysql>grantallongbk.*totest@'localhost'identifiedby'admin123';#建立用户test,用户gbk数据库所有权限,同上
- mysql>showgrantsfor'test'@'localhost';
- +-------------------------------------------------------------------------------------------------------------+
- |Grantsfortest@localhost|--phpfensi.com
- +-------------------------------------------------------------------------------------------------------------+
- |GRANTUSAGEON*.*TO'test'@'localhost'IDENTIFIEDBYPASSWORD'*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'|
- |GRANTALLPRIVILEGESON`gbk`.*TO'test'@'localhost'|
- +-------------------------------------------------------------------------------------------------------------+
- 2rowsinset(0.00sec)
- QueryOK,0rowsaffected(0.00sec)
远程连接同上:
- mysql>grantallongbk.*to'user'@'授权可连接主机'identifiedby'admin123';#这是Server端
- [root@lamp~]#/usr/local/mysql/bin/mysql-uroot-p'admin123'-hhk.t4x.org#这是client端
补充:ALL PRIVILEGES权限包括:
- mysql>showgrantsfor'byrd'@'localhost';
- +-------------------------------------------------------------------------------------------------------------+
- |Grantsforbyrd@localhost|
- +-------------------------------------------------------------------------------------------------------------+
- |GRANTUSAGEON*.*TO'byrd'@'localhost'IDENTIFIEDBYPASSWORD'*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'|
- |GRANTALLPRIVILEGESON`gbk`.*TO'byrd'@'localhost'|
- +-------------------------------------------------------------------------------------------------------------+
- mysql>revokeinserton`gbk`.*from'byrd'@'localhost';
- QueryOK,0rowsaffected(0.00sec)
- mysql>showgrantsforbyrd@'localhost';
- +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- |Grantsforbyrd@localhost|
- +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- |GRANTUSAGEON*.*TO'byrd'@'localhost'IDENTIFIEDBYPASSWORD'*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'|
- |GRANTSELECT,UPDATE,DELETE,CREATE,DROP,REFERENCES,INDEX,ALTER,CREATETEMPORARYTABLES,LOCKTABLES,EXECUTE,CREATEVIEW,SHOWVIEW,CREATEROUTINE,ALTERROUTINE,EVENT,TRIGGERON`gbk`.*TO'byrd'@'localhost'|
- +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- ALLPRIVILEGES权限包括:GRANTSELECT,UPDATE,DELETE,CREATE,DROP,REFERENCES,INDEX,ALTER,CREATETEMPORARYTABLES,LOCKTABLES,EXECUTE,CREATEVIEW,SHOWVIEW,CREATEROUTINE,ALTERROUTINE,EVENT,insert