新開了個項目,數據庫也想新搞個用戶,先登陸mysql,看看原來都有哪些:
root@wlf:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 76766 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select host,user from mysql.user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | backup | | % | hellodevelop | | % | hellocloud | | % | passerby | | % | hellomg | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 8 rows in set (0.00 sec)
我們新增一個prize用戶:
mysql> create user 'prize'@'%' identified by 'prize'; Query OK, 0 rows affected (0.11 sec) mysql> select host,user from mysql.user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | backup | | % | hellodevelop | | % | hellocloud | | % | passerby | | % | prize | | % | hellomg | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 9 rows in set (0.00 sec)
但這會兒新用戶是沒有任何操作權限的,除了看看:
mysql> show grants for 'prize'@'%'; +-----------------------------------+ | Grants for prize@% | +-----------------------------------+ | GRANT USAGE ON *.* TO 'prize'@'%' | +-----------------------------------+ 1 row in set (0.00 sec)
所以我們還得給它賦予各種權限:
mysql> grant all privileges on `prize`.* to 'prize'@'%'; Query OK, 0 rows affected (0.04 sec) mysql> show grants for 'prize'@'%'; +--------------------------------------------------+ | Grants for prize@% | +--------------------------------------------------+ | GRANT USAGE ON *.* TO 'prize'@'%' | | GRANT ALL PRIVILEGES ON `prize`.* TO 'prize'@'%' | +--------------------------------------------------+ 2 rows in set (0.00 sec)
刷新一下權限:
mysql> flush privileges; Query OK, 0 rows affected (0.09 sec)
現在可以進入prize用戶開始我們的倒騰了:
mysql> exit; Bye root@d5afa9102517:/# mysql -uprize -pprize mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 77055 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database prize; Query OK, 1 row affected (0.09 sec)