How to Add MySQL Accounts - dummies

By Steve Suehring, Janet Valade

The preferred way to access MySQL from PHP is to set up an account specifically for this purpose with only the privileges that are needed.

If you’re using an account given to you by a company IT department or a web hosting company, it might or might not have all the privileges needed to create an account. If it doesn’t, you can’t successfully execute the statement to add an account, and you have to request a second account to use with PHP.

If you need to request a second account, get an account with restricted privilege (if at all possible) because your web database application is more secure if the account your PHP programs use doesn’t have more privileges than are necessary.

To create one or more users when you have the necessary privileges, you can use the CREATE USER statement (added to MySQL in version 5.0.2), as follows:

CREATE USER accountname@hostname IDENTIFIED BY 'password',
accountname@hostname IDENTIFIED BY 'password',...

This statement creates the specified new user account(s) with the specified password for each account and no privileges. You don’t need to specify a password. If you leave out IDENTIFIED BY password, the account is created with no password. You can add or change a password for the account at a later time.

If you’re using a version of MySQL before 5.0.2, you must use a GRANT statement to create an account.