I want to create a new user in MySQL and give it full access only to one database, say dbTest
, that I create with a command like create database dbTest;
. What would be the MySQL commands to do that?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
Syntax
To create user in MySQL/MariaDB 5.7.6 and higher, use
CREATE USER
syntax:then to grant all access to the database (e.g.
my_db
), useGRANT
Syntax, e.g.Where
ALL
(priv_type) can be replaced with specific privilege such asSELECT
,INSERT
,UPDATE
,ALTER
, etc.Then to reload newly assigned permissions run:
Executing
To run above commands, you need to run
mysql
command and type them into prompt, then logout byquit
command or Ctrl-D.To run from shell, use
-e
parameter (replaceSELECT 1
with one of above commands):or print statement from the standard input:
If you've got Access denied with above, specify
-u
(for user) and-p
(for password) parameters, or for long-term access set your credentials in~/.my.cnf
, e.g.Shell integration
For people not familiar with MySQL syntax, here are handy shell functions which are easy to remember and use (to use them, you need to load the shell functions included further down).
Here is example:
To use above commands, you need to copy&paste the following functions into your rc file (e.g.
.bash_profile
) and reload your shell or source the file. In this case just typesource .bash_profile
: