MySQL添加新用户、为用户创建数据库、为新用户分配权限

登录MySQL

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mysql -u root -p  

添加新用户

允许本地 IP 访问 localhost, 127.0.0.1

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. create user 'test'@'localhost' identified by '123456';  

允许外网 IP 访问

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. create user 'test'@'%' identified by '123456';  

刷新授权

[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. flush privileges;  

为用户创建数据库

[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. create database test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;  

为新用户分配权限

授予用户通过外网IP对于该数据库的全部权限

[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. grant all privileges on `testdb`.* to 'test'@'%' identified by '123456';  
授予用户在本地服务器对该数据库的全部权限

[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. grant all privileges on `testdb`.* to 'test'@'localhost' identified by '123456';  

刷新权限
[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. flush privileges;  
退出 root 重新登录
[sql]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. exit  
用新帐号 test 重新登录,由于使用的是 % 任意IP连接,所以需要指定外部访问IP
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mysql -u test -h 115.28.203.224 -p  


在Ubuntu服务器下,MySQL默认是只允许本地登录,因此需要修改配置文件将地址绑定给注释掉:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # Instead of skip-networking the default is now to listen only on  
  2. # localhost which is more compatible and is not less secure.  
  3. #bind-address       = 127.0.0.1     #注释掉这一行就可以远程登录了 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值