新开了个项目,数据库也想新搞个用户,先登陆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)