mysql创建用户,并授予权限

mysql> GRANT ALL PRIVILEGES ON *.* TO jiqing@"%" IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

这两条指令的作用,是创建用户jiqing,密码是123456。所有ip都可以访问。并授予这个用户所有的权限,刷新生效。

同时在/etc/hosts中,将阿里云的mysql地址加入其中。

127.0.0.1    rm-wz94y9juv07017s60.mysql.rds.aliyuncs.com
关于hosts,我们本机有hosts,路由中也有hosts,路由中设置了hosts,就相当于所有的电脑都设置了。
jiqing@ubuntu:/usr/local/nginx/conf/vhost$ ping jiqing.sheep.5hao.com
PING jiqing.sheep.5hao.com (192.168.199.126) 56(84) bytes of data.
64 bytes from jiqing.hotel.caomall.net (192.168.199.126): icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from jiqing.hotel.caomall.net (192.168.199.126): icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from jiqing.hotel.caomall.net (192.168.199.126): icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from jiqing.hotel.caomall.net (192.168.199.126): icmp_seq=4 ttl=64 time=0.052 ms
jiqing@ubuntu:/usr/local/nginx/conf/vhost$ ping local.sheep.5hao.com
PING local.sheep.5hao.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.053 ms

这两个都是路由中的hosts设置的,如果我把自己本地的jiqing.sheep.5hao.com更改成192.168.199.127。看看效果。

jiqing@ubuntu:/usr/local/nginx/conf/vhost$ ping jiqing.sheep.5hao.com
PING jiqing.sheep.5hao.com (192.168.199.127) 56(84) bytes of data.
From jiqing.hotel.caomall.net (192.168.199.126) icmp_seq=1 Destination Host Unreachable
From jiqing.hotel.caomall.net (192.168.199.126) icmp_seq=2 Destination Host Unreachable
From jiqing.hotel.caomall.net (192.168.199.126) icmp_seq=3 Destination Host Unreachable
From jiqing.hotel.caomall.net (192.168.199.126) icmp_seq=4 Destination Host Unreachable

会发现,如果ping不通,它就会去路由中找。
再次改为127.0.0.1

jiqing@ubuntu:/usr/local/nginx/conf/vhost$ ping jiqing.sheep.5hao.com
PING jiqing.sheep.5hao.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.047 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.052 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.046 ms

会发现,如果ping通了,就优先执行本地的ip。很有意思。

这样就可以实现,同样的代码。可以连接本地的数据库。即便上传到了服务器,一样可以正常使用。

这其实是造假吧,访问的并不是阿里云的mysql数据库,而是本地的数据库。

线上的服务器,也要这么造假,访问的也不是阿里云的数据库,而是它自己的数据库。

经过这样造假之后,本地和线上的数据库配置就都不需要更改了。

    'DB_HOST'                   =>  'rm-wz94y9juv07017s60.mysql.rds.aliyuncs.com', // 数据库host
    'DB_NAME'                   =>  'xxx',                                    // db name
    'DB_USER'                   =>  'jiqing',                                  // 用户名
    'DB_PWD'                    =>  '123456',                               // 密码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值