0

I just opened mysql by typing "mysql" in the terminal. Then, I went to create a database which threw this error :-

mysql> create database test2;
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'test2'
mysql>

How do I fix this ? P.S - I've installed LAMP using tasksel. Thanks.

2 Answers 2

1

Try this. Start MySQL as root, just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:

mysql -u root -p
0
0

As you probably know, MySQL is a database server. That means, you need a client to connect to a MySQL server. The mysql command line program you are using is such a client.

As with many systems, a MySQL server has its own user accounts and privileges. So if you connect a client to a MySQL server, you have to provide an username and (almost always) a password. If you don't provide the mysql with an username, it assumes you want to use the UNIX/Linux username you are logged into your server as. For example, if you are logged into your server as 'joe', mysql tries to use the MySQL user of the same name, which is 'joe@localhost'. As you see, MySQL user accounts always contain the hostname you are connecting from. That is automatically added.

If you want to connect to your MySQL server as the user 'joe@localhost', you can do

mysql -u joe

With this, MySQL assumes that the MySQL server is running on the same machine as the client (therefore, the 'localhost' hostname is automatically added) and 'joe@localhost' does not have a password set.

If the desired MySQL user account has a password set, you can have the mysql client ask for it with

mysql -u joe -p

And, for the sake of completeness, if you want to connect to a MySQL on another machine as the client runs on, you can do something like

mysql -h my-mysql-server.com -u joe -p

In your special case, the MySQL user account the mysql client connects to the MySQL server as does not have the necessary privileges to create a new database. You need to connect as a MySQL user that has these privileges. Which user account that is depends on how you set the MySQL server up.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .