How to create a MySQL database

Here we will show how to install and create a MySQL database. We will also create a MySQL user with access to our new MySQL database. We will GRANT our user all privileges on using the database.

Install mysql

Install mysql-server from your distro or using apt-get install mysql-server. During installation you a password for root is set.

If you get this error message you should install mysql-server.
> mysql
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Connect to MySQL

After you have installed mysql you should be able to connect to mysql.
> mysql
ERROR 1045 (28000): Access denied for user ‘xxx’@’localhost’ (using password: NO)
This error is due to no user with name ‘xxx’.

Connect by providing username and password:
> mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.0.75-0ubuntu10 (Ubuntu)

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

mysql>

End your session by typing either quit or exit at mysql>

Create a MySQL database

We are connected to mysql as root.
In the next line we create a mysql database with name ‘wordpress_db’

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

mysql>

Create a MySQL user

Let’s create a user ‘wp_user’ with a given password ‘mypassword’ at ‘localhost’.

mysql> CREATE USER ‘wp_user’@’localhost’ IDENTIFIED BY ‘mypassword’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Grant user all priveleges on your MySQL database

mysql> grant all on wordpress_db.* to ‘wp_user’@’localhost’;
Query OK, 0 rows affected (0.00 sec)

mysql>

2 thoughts on “How to create a MySQL database

Comments are closed.