北城已荒凉 2009-11-12 06:10 采纳率: 100%
浏览 254

在 MySQL 中创建新用户,并赋予其对一个数据库的完全访问权

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?

转载于:https://stackoverflow.com/questions/1720244/create-new-user-in-mysql-and-give-it-full-access-to-one-database

  • 写回答

7条回答 默认 最新

  • 零零乙 2009-11-12 06:14
    关注

    Try this to create the user:

    CREATE USER 'user'@'hostname';
    

    Try this to give it access to the database dbTest:

    GRANT ALL PRIVILEGES ON dbTest.* To 'user'@'hostname' IDENTIFIED BY 'password';
    

    If you are running the code/site accessing MySQL on the same machine, hostname would be localhost.

    Now, the break down.

    GRANT - This is the command used to create users and grant rights to databases, tables, etc.

    ALL PRIVILEGES - This tells it the user will have all standard privileges. This does not include the privilege to use the GRANT command however.

    dbtest.* - This instructions MySQL to apply these rights for use in the entire dbtest database. You can replace the * with specific table names or store routines if you wish.

    TO 'user'@'hostname' - 'user' is the username of the user account you are creating. Note: You must have the single quotes in there. 'hostname' tells MySQL what hosts the user can connect from. If you only want it from the same machine, use localhost

    IDENTIFIED BY 'password' - As you would have guessed, this sets the password for that user.

    评论

报告相同问题?