How to Create, Grant, Delete Users, and Change Passwords in MySQL

First, a disclaimer: Generally, modifying MySQL passwords and granting privileges requires root access in MySQL.

Note: This operation is performed under the Windows command prompt; phpMyAdmin is equally applicable.

    User: phplamp  Database: phplampDB

1. Create a new user.

//Log into MYSQL
@>mysql -u root -p
@>Password
//Create user
mysql> insert into mysql.user(Host,User,Password) values("localhost","phplamp",password("1234"));
//Flush privileges
mysql>flush privileges;
This creates a user named: phplamp  with password: 1234  .
Then try logging in.
mysql>exit;
@>mysql -u phplamp -p
@>Enter password
mysql>Login successful

2. Grant privileges to the user.
//Log into MYSQL (with ROOT privileges). Here I log in as ROOT.
@>mysql -u root -p
@>Password
//First, create a database for the user (phplampDB)
mysql>create database phplampDB;
//Grant the phplamp user all privileges on the phplamp database.
>grant all privileges on phplampDB.* to phplamp@localhost identified by '1234";
//Flush privileges
mysql>flush privileges;
mysql>Other operations
/*
If you want to grant specific privileges to a user, you can write it like this:
mysql>grant select,update on phplampDB.* to phplamp@localhost identified by '1234";
//Flush privileges.
mysql>flush privileges;
*/

3. Delete a user.
@>mysql -u root -p
@>Password
mysql>DELETE FROM user WHERE User="phplamp" and Host="localhost";
mysql>flush privileges;
//Drop the user’s database
mysql>drop database phplampDB;

4. Change a specific user’s password.
@>mysql -u root -p
@>Password
mysql>update mysql.user set password=password('NewPassword') where User="phplamp" and Host="localhost";
mysql>flush privileges;

This article is from a ChinaUnix blog. Click to view the original post:http://blog.chinaunix.net/u2/62871/showart_1924660.html

Leave a Comment

Your email address will not be published.