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?
After you've installed MySQL, you need to set mysql root password. To do so:
-
Enter the next command in a terminal:
mysql -u root
-
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:
-
Open mysql from terminal:
mysql -u root -p
Enter the password created before.
-
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!
You need to connect to MySQL using the root user and associated password. If you need to set them, use the following command: sudo mysqladmin -u root -h localhost password 'mypassword'
From there, you can configure additional accounts by following this document: How to Create a New User and Grant Permissions in MySQL