MySQL: Creating a Database

Yesterday I installed MySQL 5.1.30 on my computer. As I haven’t used MySQL before, I played a bit around with it today and read some of the online documentation.

Some basic steps are to create a new user, a new database, and assign all access rights for the new database to the new user. This is what I need to do, when installing a new program which requires a MySQL DB to store data in. All the required information can be found in the online documentation, however, here’s a summary of the steps to do:

mbp:~ gerhard$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.30 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create user sam identified by 'samspassword';
Query OK, 0 rows affected (0.00 sec)

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on testdb.* to 'sam';
Query OK, 0 rows affected (0.00 sec)

That’s it, just three commands to remember…

I connected to MySQL with the root user. Then created a new user “sam” with password “samspassword”. Next I created a new database named “testdb”. And in the last step I gave all rights on this database to user sam.

Afterwards, Sam can log in to his database “testdb” like this:

mbp:~ gerhard$ mysql -u sam -psamspassword testdb

Comments are closed.