42

I've just installed xampp, and am using command line to write mySQL. I am using 'root' with no password and can connect to mysql but cannot CREATE DATABASE as I get the error 1044 access denied for user '' @ 'localhost'. I am logged in as -uroot.

I have privileges in phpMyadmin to do what I want, but, in command line I seem to have no write privileges. I've looked at all the other related posts on this topic but to no avail. I cannot GRANT privileges as I have none anyway.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
gavin stanley
  • 1,082
  • 2
  • 13
  • 28
  • On MySQL commandline tool, when you click it, it opens and asks for the password, are you able to successfully log in? – jsist Aug 01 '12 at 13:25
  • Hi. I am not prompted for anything, I just navigate to C:\xampp\mysql\bin\mysql -u root; – gavin stanley Aug 01 '12 at 13:28
  • This returns "Welcome to mySQL monitor" etc. Then if I type SHOW DATABASES I get +--------------------+|Database|+----------------+|Information_schema|+-------------------+|test||+----------------+, so I am connected to MySQL – gavin stanley Aug 01 '12 at 13:31
  • but when I try CREATE DATABASE, I get the 'access denied' error – gavin stanley Aug 01 '12 at 13:32
  • 1
    I'm voting to close this question as off-topic because software based nothing to do with programming. – Evan Carroll Nov 12 '15 at 00:25

9 Answers9

73

Are you logging into MySQL as root? You have to explicitly grant privileges to your "regular" MySQL user account while logged in as MySQL root.

First set up a root account for your MySQL database.

In the terminal type:

mysqladmin -u root password 'password'

To log into MySQL, use this:

mysql -u root -p

To set the privileges manually start the server with the skip-grant-tables option, open mysql client and manually update the mysql.user table and/or the mysql.db tables. This can be a tedious task though so if what you need is an account with all privs I would do the following.

Start the server with the skip-grant-tables option

Start mysql client (without a username/password)

Issue the command

flush privileges;

which forces the grant tables to be loaded.

Create a new account with the GRANT command something like this (but replacing username and password with whatever you want to use.

GRANT ALL on *.* to 'username'@'localhost' identified by 'password';

Restart the server in normal mode (without skip-grant-tables) and log in with your newly created account.

Refer this MySQL docs.

Jacob
  • 2,041
  • 14
  • 16
  • Thanks, Jacob. Actually I re-installed the ODBC connector msi and re-installed mySQL directly (aside from xampp) and it now works. It was a connector problem I think, as SHOW DATABASES wasn't actually showing my databases at all. My 'root' login wasn't getting access to the DB, which made it seem like it had limited priviliges but it wasn't connected properly. This is useful stuff going forward, though - thanks. – gavin stanley Aug 02 '12 at 11:24
  • One of my friend (he is still new to mysql) run into the same issue, after he `created user root`... Just another creative senario for reference. – Ben Aug 14 '19 at 00:52
13

navigate do C:\xampp\mysql\bin\ and make sure the file mysql.exe is in that folder.

mysql -uroot -p

if dont have a password just press enter.

the prompt changes to

mysql>

do your mysql commands

Paulo Pinto
  • 194
  • 1
  • 8
  • btw I was using windows command line and now using mysql command line. Either way, they are the same thing – gavin stanley Aug 01 '12 at 13:53
  • @gavin can you run this command "SELECT USER(),CURRENT_USER();" ? – Paulo Pinto Aug 01 '12 at 13:55
  • USER: ODBC@localhost, CURRENT_USER:@localhost – gavin stanley Aug 01 '12 at 13:58
  • you need to make this command to log as root: "mysql -uroot -p" ... NO space between -u and root – Paulo Pinto Aug 01 '12 at 14:03
  • make sure you start the xampp service. – Paulo Pinto Aug 01 '12 at 14:04
  • if you get this USER: ODBC@localhost, CURRENT_USER:@localhost, you are not root... when execute this SELECT USER(),CURRENT_USER(); for root you get something like USER: root@localhost, CURRENT_USER:root@localhost – Paulo Pinto Aug 01 '12 at 14:12
  • Ah, so I am not logged in as root. That would explain lack of privileges. But I don't understand why I am not root. I need to CHANGE USER – gavin stanley Aug 01 '12 at 14:15
  • @gavin if prompt is mysql> write exit and enter... then where prompt is c:\xampp\...bin\ write mysql -uroot -p and enter... and the prompt will turn to mysql> – Paulo Pinto Aug 01 '12 at 14:20
  • @gavin sorry about that.. 1) close command line and open again; 2) go to c:\xampp\...\mysql\...\bin\ ; 3) run this command: mysql -uroot -p; 4) your prompt shoud be link mysql> 5) run this command: SELECT USER(),CURRENT_USER(); – Paulo Pinto Aug 01 '12 at 14:33
  • ok. If I run the mysql.exe directly and select user i get ODBC@localhost. If I run windows commmand-line and go to the mysql, and SELECT USER, I get 'root@localhost'. However, I still get the access denied error when typing CREATE DATABASE. I have checked the priveliges in phpMyAdmin and everything is checked for root. Totally Stumped! – gavin stanley Aug 01 '12 at 14:45
5

By default there is no password is set for root user in XAMPP.

You can set password for root user of MySQL.

Navigate to

localhost:80/security/index.php

and set password for root user.

Note:Please change the port number in above url if your Apache in on different port.

Open XAMPP control panel Click "Shell" button

Command prompt window will open now in that window type

mysql -u root -p;

It will ask for password type the password which you have set for root user.

There you go ur logged in as root user :D Now do what u want to do :P

Vishal
  • 639
  • 7
  • 32
4

Gain access to a MariaDB 10 database server

After stopping the database server, the next step is to gain access to the server through a backdoor by starting the database server and skipping networking and permission tables. This can be done by running the commands below.

sudo mysqld_safe --skip-grant-tables --skip-networking &

Reset MariaDB root Password

Now that the database server is started in safe mode, run the commands below to logon as root without password prompt. To do that, run the commands below

sudo mysql -u root

Then run the commands below to use the mysql database.

use mysql;

Finally, run the commands below to reset the root password.

update user set password=PASSWORD("new_password_here") where User='root';

Replace new_password _here with the new password you want to create for the root account, then press Enter.

After that, run the commands below to update the permissions and save your changes to disk.

flush privileges;

Exit (CTRL + D) and you’re done.

Next start MariaDB normally and test the new password you just created.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service

Logon to the database by running the commands below.

sudo mysql -u root -p

source: https://websiteforstudents.com/reset-mariadb-root-password-ubuntu-17-04-17-10/

rubo77
  • 19,527
  • 31
  • 134
  • 226
1

I had the same issue, and it turned out to be that MariaDB was set to allow only root to log in locally via the unix_socket plug-in, so clearing that setting allowed successfully logging in with the user specified on the command line, provided a correct password is entered, of course. See this answer on Ask Ubuntu

A. Gh.
  • 13
  • 6
  • Note that since MariaDB 10.4, it is possible to set user accounts so that they can log using either of multiple authentication methods.See https://stackoverflow.com/a/65544967/9758383/ – A. Gh. Dec 06 '21 at 12:25
0

I re-installed the ODBC connector msi and re-installed mySQL directly (aside from xampp) and it now works. It was a connector problem I think, as SHOW DATABASES wasn't actually showing my databases at all.

My 'root' login wasn't getting access to the DB, which made it seem like it had limited priviliges but it actually wasn't connected properly.

gavin stanley
  • 1,082
  • 2
  • 13
  • 28
0

Server file only change name folder

etc/mysql
rename
mysql-
rubo77
  • 19,527
  • 31
  • 134
  • 226
alpc
  • 598
  • 3
  • 6
-1

this might help on Ubuntu:

go to /etc/mysql/my.cnf and comment this line:

bind-address           = 127.0.0.1

Hope this helps someone, I've been searching for this a while too

Cheers

Tom
  • 1
-2

You mustn't have a space character between -u and the username:

mysql -uroot -p
# or
mysql --user=root --password
feeela
  • 29,399
  • 7
  • 59
  • 71
  • Hi. I have just tried that and I still get the same error. Should it say: "Acess denied for 'root'@'localhost'"? instead of user:''? I'm not sure I'm logged in as 'root' – gavin stanley Aug 01 '12 at 13:41