Ubuntu – How to connect and create a database in MySQL


I want to install MySQL and create a database on it using the following code:

sudo apt-get install mysql-server
mysqladmin -h localhost -u {username} -p create lrs

I get the following message after executing the second line:

    Enter password:
    mysqladmin: connect to server at 'localhost' failed
    error: 'Access denied for user '{username}'@'localhost' (using password: YES)'

What is the problem?

Best Answer

After you've installed MySQL, you need to set mysql root password. To do so:

  1. Enter the next command in a terminal:

    mysql -u root

  2. Now it should open the mysql console. And type the following line:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

To exit from the mysql console enter exit.

Now you should create the database with the root user. To do so:

  1. Open mysql from terminal:

    mysql -u root -p

  2. Enter the password created before.

  3. Enter the following line:

    CREATE DATABASE yourdatabasename;

If you enter SHOW DATABASES; you should see it in the list. If so, you have a database ready to use!