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',                               // 密码
原文地址:https://www.cnblogs.com/jiqing9006/p/8872674.html