Blog by Edo Frederix edofrederix@gmail.com RSS

Creating a MySQL database with proper rights

February 28, 2008

Abstract

Sometimes it’s easy to create a database without any specific permissions, to test some scripts on. Find out how.

To simply create a MySQL database, phpMyAdmin is your friend. It lets you create a new user, and with that user you may automatically create a database with proper permissions. In some cases however, you do not have access to phpMyAdmin, for whatever the reason may be.

To make things easy though, here are the MySQL commands that phpMyAdmin uses:

CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'pass';
GRANT USAGE ON * . * TO 'testuser'@'localhost' IDENTIFIED BY 'pass';
CREATE DATABASE IF NOT EXISTS `testDB` ;
GRANT ALL PRIVILEGES ON `testDB` . * TO 'testuser'@'localhost';

Now we have created a database called “testDB”, with a user named “testuser” and password “pass”, having proper rights from localhost. You can execute this MySQL code via a PHP script or the MySQL command line client.