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',                               // 密碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM