197

I want to begin writing queries in MySQL.

show grants shows:

+--------------------------------------+
| Grants for @localhost                |
+--------------------------------------+
| GRANT USAGE ON *.* TO ''@'localhost' |
+--------------------------------------+

I do not have any user-id but when I want to make a user I don't have privilleges, also I don't know how to make privileges when even I don't have one user!

mysql> CREATE USER 'parsa'@'localhost' IDENTIFIED BY 'parsa';
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER pr
ivilege(s) for this operation

I tried to sign in as root:

mysql> mysql -u root -p;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
 -u root -p' at line 1
mysql> mysql -u root -p root;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
 -u root -p root' at line 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Nickool
  • 3,662
  • 10
  • 42
  • 72
  • 3
    You need to log in as a user with permissions. At least `root` should have been created when you installed MySQL. – derobert Jan 12 '12 at 16:46
  • during installation it tells me to set password and i set it to root now what can I do? – Nickool Jan 12 '12 at 16:47
  • 11
    You need to log in as root—e.g., by running `mysql -u root -p`. Then you'll have full permissions on the database server, and you can create other users. – derobert Jan 12 '12 at 16:49
  • when you log in using command line, do the following to log in as root: mysql -u root -p – Travis Jan 12 '12 at 16:49
  • I edited I tried it in the past maybe my syntax is not correct I dunno – Nickool Jan 12 '12 at 16:52
  • 2
    @nikparsa: No, you'd run that instead of 'mysql'—from the shell prompt, not at the mysql prompt. – derobert Jan 12 '12 at 16:57
  • @derpbert what should i do now exactly? – Nickool Jan 12 '12 at 17:00
  • @nikparsa: If you're on Windows, I think there is a different start menu entry (should have 'root' in its name, I believe). Or run it from the `cmd` prompt. On Unix/Linux, type `mysql -u root -p` in a terminal (at the bash prompt). On Mac OS X, pull up a Terminal.app and also type `mysql -u root -p`. – derobert Jan 12 '12 at 17:01
  • I tried root with quotation too I am in windows xp I hate xp! linux is so better! – Nickool Jan 12 '12 at 17:02
  • This is because you haven't login to MySQL using root user. Try login using "mysql -u root". – Praveen Kishor Apr 18 '16 at 12:34
  • Heads up: If you attempt to use a database with an incorrectly typed name, it will throw this error. Make sure that you've entered your database name correctly. – Dodgie Dec 13 '16 at 04:02
  • you have to type `exit;` to go back to bash before you can start mysql as the root user: `mysql -u root -p` – b264 Oct 10 '18 at 18:24

12 Answers12

238

No, you should run mysql -u root -p in bash, not at the MySQL command-line. If you are in mysql, you can exit by typing exit.

Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
Nowhy
  • 2,894
  • 1
  • 16
  • 13
  • how can I go to bash?I had problem installing mysql see here http://superuser.com/questions/377679/mysql-client-not-working-windows-xp I just know abt it in command line – Nickool Jan 12 '12 at 17:01
  • 8
    use 'quit' to exit the mysql,then you will be in bash – Nowhy Jan 12 '12 at 17:02
  • 29
    FYI by default the password is blank – bigpotato Jun 19 '13 at 15:04
  • 1
    What if I am using windows? – PHPFan May 17 '17 at 07:39
  • exit or quit is not working for me, please provide another way. I'm on windows 10 – Metin Dagcilar Aug 07 '17 at 12:35
  • @PHPFan and posterity, Even for windows, its same. From, command prompt enter `mysql -u root -p` – lupchiazoem Feb 12 '19 at 04:01
  • If `exit` won't work, you're probably still in the process of typing an instruction so mysql is interpreting `exit` as part of that instruction and not the `exit` keyword (should be seeing `->` when you hit enter if this is the case). Try typing a semicolon and hit enter to terminate the instruction; then the next line (that should start with `mysql>` and NOT `->` and it will respond appropriately to the `exit` keyword. – Allison Jul 18 '19 at 01:28
  • You can also try `ctrl+d` to leave MySQL. – chenghuayang Jul 27 '21 at 13:08
55

You may need to set up a root account for your MySQL database:

In the terminal type:

mysqladmin -u root password 'root password goes here'

And then to invoke the MySQL client:

mysql -h localhost -u root -p
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
soleshoe
  • 1,215
  • 2
  • 12
  • 16
  • 2
    This one worked for me, it was working fine initially and then suddenly stopped working one day. Any idea, why this happens in the first place? – Anshul Goyal Feb 04 '14 at 10:29
  • @soleshoe i m getting error : sudo: mysqladmin: command not found please help me what can i do?? mysql is alreay running in System Preference. – Hitarth Sep 01 '15 at 17:51
  • I get: mysqladmin: Can't turn off logging; error: 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation' – Gubatron Aug 07 '20 at 20:28
32

I was brought here by a different problem. Whenever I tried to login, i got that message because instead of authenticating correctly I logged in as anonymous user. The solution to my problem was:

To see which user you are, and whose permissions you have:

select user(), current_user();

To delete the pesky anonymous user:

drop user ''@'localhost';
Lefteris E
  • 2,806
  • 1
  • 24
  • 23
  • 2
    This solved the problem for me even though I was using "mysql -u username -p". Very strange: user() was my username, but current_user() was localhost... dont know why? – ethanpil Jul 02 '14 at 18:30
  • 3
    @Leftteris hi .. i m getting this error : Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation When i try to drop user ''@'localhost'; – Hitarth Sep 01 '15 at 17:14
  • +-----------------+----------------+ | user() | current_user() | +-----------------+----------------+ | fantomx1@localhost | @localhost | +-----------------+----------------+ | thx, I got this, when the anonymous user existing, I could login with whatever existing user without password even when they had password but their password didnt work, but they didnt have privileges, or even with any non existing thinked out string user 'sdasdsa', but the current user was always anonymous, after dropping him it started to work – FantomX1 Sep 26 '20 at 12:00
  • this site has info on how to drop the anonymous user. http://download.nust.na/pub6/mysql/doc/refman/5.1/en/default-privileges.html – Lefteris E Jul 31 '21 at 22:36
28

This is something to do with user permissions. Giving proper grants will solve this issue.

Step [1]: Open terminal and run this command

$ mysql -uroot -p

Output [1]: This should give you mysql prompt shown below

enter image description here

Step [2]:

mysql> CREATE USER 'parsa'@'localhost' IDENTIFIED BY 'your_password';
mysql> grant all privileges on *.* to 'parsa'@'localhost';

Syntax:

mysql> grant all privileges on `database_name`.`table_name` to 'user_name'@'hostname';

Note:

  • hostname can be IP address, localhost, 127.0.0.1
  • In database_name/table_name, * means all databases
  • In hostname, to specify all hosts use '%'

Step [3]: Get out of current mysql prompt by either entering quit / exit command or press Ctrl+D.

Step [4]: Login to your new user

$ mysql -uparsa -pyour_password

Step [5]: Create the database

mysql> create database `database_name`;
theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26
  • 2
    when I do `CREATE USER 'parsa'@'localhost' IDENTIFIED BY 'your_password';` there get error: ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation – aircraft Jul 18 '18 at 09:24
  • Have you logged into mysql client using root user – theBuzzyCoder Jul 19 '18 at 04:33
  • This solved my issue: `grant all privileges on *.* to 'parsa'@'%';` . I'm using docker-compose, is there a way I can pass this command after build? – Fabio Magarelli Oct 05 '20 at 10:21
  • I encountered the same problem when I migrated my existing database from phpmyadmin xampp to mysql. This finally worked for my node.js mysql app – leipzy Mar 09 '21 at 06:57
  • This is the best answer on this topic! – RecharBao Aug 22 '21 at 11:09
11

You might want to try the full login command:

mysql -h host -u root -p 

where host would be 127.0.0.1.

Do this just to make sure cooperation exists.

Using mysql -u root -p allows me to do a a lot of database searching, but refuses any database creation due to a path setting.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ray
  • 111
  • 1
  • 2
7

First, if you are unfamiliar with the command line, try using phpmyadmin from your webbrowser. This will make sure you actually have a mysql database created and a username.

This is how you connect from the command line (bash):

mysql -h hostname -u username -p database_name

For example:

fabio@crunchbang ~ $ mysql -h 127.0.0.1 -u fabio -p fabiodb
fedorqui
  • 275,237
  • 103
  • 548
  • 598
fabiog1901
  • 352
  • 3
  • 12
  • 1
    `-p` is for password and you are using it to pass database name. Also, for password, there **shouldn't** be a space b/w `-p` and the actual password. Use `-D` to connect to a specific database. – Say No To Censorship Jan 14 '16 at 18:30
7

If you are in a MySQL shell, exit it by typing exit, which will return you to the command prompt.

Now start MySQL by using exactly the following command:

sudo mysql -u root -p

If your username is something other than root, replace 'root' in the above command with your username:

sudo mysql -u <your-user-name> -p

It will then ask you the MySQL account/password, and your MySQL won't show any access privilege issue then on.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
7

connect mysql with sudo & gives permission for the necessary user using,

sudo mysql -u user;
GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'localhost';
Kaumadie Kariyawasam
  • 1,232
  • 3
  • 17
  • 35
2

@Nickparsa … you have 2 issues:

1). mysql -uroot -p should be typed in bash (also known as your terminal) not in MySQL command-line. You fix this error by typing

exit

in your MySQL command-line. Now you are back in your bash/terminal command-line.

2). You have a syntax error:

mysql -uroot -p; 

the semicolon in front of -p needs to go. The correct syntax is:

mysql -uroot -p

type the correct syntax in your bash commandline. Enter a password if you have one set up; else just hit the enter button. You should get a response that is similar to this: enter image description here

Hope this helps!

Uzzar
  • 705
  • 1
  • 12
  • 24
  • Hi i got the abow screen direcly after fire this commend. /usr/local/mysql/bin/mysql but after that when i try to create database using " CREATE DATABASE books; " i am getting error : ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'books' please help me – Hitarth Sep 01 '15 at 17:49
0

Most Developers log-in to server(I assume you r having user-name and password for mysql database) then from Bash they switch to mysql> prompt then use the command below(which doesn’t work

mysql -h localhost -u root -p

What needs to be done is use the above command in the bash prompt--> on doing so it will ask for password if given it will take directly to mysql prompt and

then database, table can be created one by one

I faced similar deadlock so sharing the experience

Devrath
  • 42,072
  • 54
  • 195
  • 297
0

I had the command correct per above answers, what I missed on was on the Workbench, where we mention 'Limit Connectivity from Host' for the user, it defaults to "%" - change this to "localhost" and it connects fine thereafter!

killjoy
  • 940
  • 1
  • 11
  • 16
0

I'm using roles to confer least privilege on my database application users. I kept getting 'ERROR 1044 (42000): Access denied for user...' until I RTFM and discovered I had to give each user a default role(s) in order their account could be authenticated when they logged in.

#create a role
CREATE ROLE 'rolename';

#give necessary privileges to role
GRANT INSERT, UPDATE, DELETE, SELECT ON database.table TO 'rolename';

#create user
CREATE USER 'username'@'host' IDENTIFIED BY 'password';

#give the user a role(s)
GRANT 'rolename' TO 'username'@'host';

#set the user's default otherwise it's ERROR 1044
SET DEFAULT ROLE 'rolename' FOR 'username'@'host';
Clarius
  • 1,183
  • 10
  • 10