I just installed mysql on a mac and I can't seem to access any accounts from there.
I used the commands :
shell> mysql -u root -p
password:
and since I have not given one I let it go blank and I get an error saying incorrect password.
I can login using just
shell> mysql
buy I can't seem to be able to change passwords or even look at all the accounts in mysql.users.
I get the following error:
ERROR 1142 (42000): SELECT command denied to user ''#'localhost' for table 'users'
What do I do to resolve this issue and how do I use a software like sequel pro with the database?
Your problem seems, you dont have all the privileges for localhost#root, to get all the privileges run the below command.
mysql> grant all privileges on *.* to root#localhost identified by 'password' with grant option
in MySQL command prompt and you will have all the previleges to access the localhost as root.
use this command to set the password for MySQL
mysqladmin -u root password “newpassword”;
Example mysqladmin -u root password adhfhuef34;
You will also want to restart the database server after running this command
sudo /etc/init.d/mysql restart
If this Dint work, Try
$ mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("NEWPASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Related
I just installed MariaDB 10.1.29 on Ubuntu 18.04. From the command line I can connect using sudo:
sudo mysql -u root -p
But not without sudo.
Also, if I try to connect to the database through DBeaver, I get:
Could not connect: Access denied for user 'root'#'localhost'
While the credentials are correct. I tried installing MySQL 5.7, but I'm experiencing the exact same issue there as well.
What am I missing?
As it turns out, this is expected behaviour for MariaDB and MySQL. To overcome the issue, it is advisable to create a separate user and grant access to all databases created. Here is a step by step guide on how to create databases using the command line and grant permissions to the user of your choice.
Log in to MariaDB/MySQL
$ sudo mysql -u root -p
Create a database
mysql> CREATE DATABASE `database_name`;
Create a user
mysql> CREATE USER 'user_name' IDENTIFIED BY 'a_secure_password';
Grant user permissions
mysql> GRANT ALL PRIVILEGES ON database_name.* TO 'user_name'#'%' WITH GRANT OPTION;
Apply changes
mysql> FLUSH PRIVILEGES;
The command:
mysql -u root -p
gives the error:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
But running sudo privileges, works:
sudo mysql -u root -p
Is it possible to get rid of the sudo requirement because it prevents me from opening the database in intellij? I tried the following as in the answer to this question Connect to local MySQL server without sudo:
sudo chmod -R 755 /var/lib/mysql/
which did not help. The above question has a different error thrown
Only the root user needs sudo requirement to login to mysql. I resolved this by creating a new user and granting access to the required databases:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'#'localhost';
now newuser can login without sudo requirement:
mysql -u newuser -p
You need to change algorithm. Following work for me,
mysql > ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '';
mysql > FLUSH PRIVILEGES;
You can use the same ROOT user, or a NEW_USER and remove the SUDO privileges. Below example shows how to remove connect using ROOT, without SUDO.
Connect to MY-SQL using SUDO
sudo mysql -u root
Delete the current Root User from the User Table
DROP USER 'root'#'localhost';
Create a new ROOT user (You can create a different user if needed)
CREATE USER 'root'#'%' IDENTIFIED BY '';
Grant permissions to new User (ROOT)
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
Flush privileges, so that the Grant tables get reloaded immediately. (Why do we need to flush privileges?)
FLUSH PRIVILEGES;
Now it's all good. Just in case, check whether a new root user is created.
SELECT User,Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| root | % |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
Exit mysql. (Press CTRL + Z). Connect to MySQL without SUDO
mysql -u root
Hope this will help!
first login to your mysql with sudo.
then use this code to change "plugin" coloumn value from "unix_socket" or "auth_socket" to "mysql_native_password" for root user.
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin IN ('unix_socket', 'auth_socket');
FLUSH PRIVILEGES;
finally restart mysql service. that's it.
if you want more info, check this link
UPDATE:
In new versions of mysql or mariadb you can use :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('your-password');
FLUSH PRIVILEGES;
I have solved this problem using following commands.
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'username'#'localhost';
FLUSH PRIVILEGES;
Here,
username = any user name you like.
and password = any password you like.
You can use the below query:
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' IDENTIFIED BY 'password';
This query is enough.
This answer needs to be slightly adapted for mariaDB instead of mysql.
First login as root using sudo:
$ sudo mysql -uroot
Then alter the mariadb root user:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('mypassword');
FLUSH PRIVILEGES;
From now on sudo is not longer needed:
$ mysql -uroot -p
Version used:
mysql Ver 15.1 Distrib 10.4.13-MariaDB, for osx10.15 (x86_64) using readline 5.1
Login to mysql with sudo:
sudo mysql -u root -p
After that Delete current root#localhost account:
~ MariaDB [(none)]> DROP USER 'root'#'localhost';
~ MariaDB [(none)]> CREATE USER 'root'#'localhost' IDENTIFIED BY 'password';
~ MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
~ MariaDB [(none)]> FLUSH PRIVILEGES;
In the comment of the question you answer you referenced, it reads
Ok, just try to analyze all of the directories down in the path of the
socket file, they need to have o+rx and the sock file too (it's not a
good idea to make it modifiable by others).
You can also try to remove mysql.sock and then restart mysqld, the
file should be created by the daemon with proper privileges.
This seemed to work for this question(the one you said you looked at) so it may work for you as well
The error Message:
"ERROR 1698 (28000): Access denied for user 'root'#'localhost'"
means that the Server not allow the connect for this user and not that mysql cant access the socket.
try this to solve the problem:
Login in your DB
sudo mysql -u root -p
then make these modifications:
MariaDB []>use mysql;
MariaDB [mysql]>update user set plugin=' ' where User='root';
MariaDB [mysql]>flush privileges;
MariaDB [mysql]>exit
try login again without sudo
Today I did a login as root into Ubuntu 14.04.1 LTS ll
and then apt-get install mariadb-server (without sudo but as root).
With mySQL -h localhost -u root --password=<PW> I got
Access denied for user 'root'#'localhost' (using password: YES)
With mySQL -u root -p I logged into the DB and did
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<PW>';
FLUSH ALL PRIVILEGES;
But this did not help. Have you got any idea?
I did not find the answer for the similar questions.
TL;DR: To access newer versions of mysql/mariadb as the root user, after a new install, you need to be in a root shell (ie sudo mysql -u root, or mysql -u root inside a shell started by su - or sudo -i first)
Having just done the same upgrade, on Ubuntu, I had the same issue.
What was odd was that
sudo /usr/bin/mysql_secure_installation
Would accept my password, and allow me to set it, but I couldn't log in as root via the mysql client
I had to start mariadb with
sudo mysqld_safe --skip-grant-tables
to get access as root, whilst all the other users could still access fine.
Looking at the mysql.user table I noticed for root the plugin column is set to unix_socket whereas all other users it is set to 'mysql_native_password'. A quick look at this page: https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/ explains that the Unix Socket enables logging in by matching uid of the process running the client with that of the user in the mysql.user table. In other words to access mariadb as root you have to be logged in as root.
Sure enough restarting my mariadb daemon with authentication required I can login as root with
sudo mysql -u root -p
or
sudo su -
mysql -u root -p
Having done this I thought about how to access without having to do the sudo, which is just a matter of running these mysql queries
GRANT ALL PRIVILEGES on *.* to 'root'#'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;
(replacing <password> with your desired mysql root password). This enabled password logins for the root user.
Alternatively running the mysql query:
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
FLUSH PRIVILEGES;
Will change the root account to use password login without changing the password, but this may leave you with a mysql/mariadb install with no root password on it.
After either of these you need to restarting mysql/mariadb:
sudo service mysql restart
And voila I had access from my personal account via mysql -u root -p
PLEASE NOTE THAT DOING THIS IS REDUCING SECURITY Presumably the MariaDB developers have opted to have root access work like this for a good reason.
Thinking about it I'm quite happy to have to sudo mysql -u root -p so I'm switching back to that, but I thought I'd post my solution as I couldn't find one elsewhere.
In clean Ubuntu 16.04 LTS, MariaDB root login for localhost changed from password style to sudo login style...
so, just do
sudo mysql -u root
since we want to login with password, create another user 'user'
in MariaDB console... (you get in MariaDB console with 'sudo mysql -u root')
use mysql
CREATE USER 'user'#'localhost' IDENTIFIED BY 'yourpassword';
\q
then in bash shell prompt,
mysql-workbench
and you can login with 'user' with 'yourpassword' on localhost
from superuser accepted answer:
sudo mysql -u root
use mysql;
update user set plugin='' where User='root';
flush privileges;
exit;
Try the command
sudo mysql_secure_installation
press enter and assign a new password for root in mysql/mariadb.
If you get an error like
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock'
enable the service with
service mysql start
now if you re-enter with
mysql -u root -p
if you follow the problem enter with sudo su and mysql -u root -p now apply permissions to root
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<password>';
this fixed my problem in MariaDB.
Good luck
I had to be logged into Ubuntu as root in order to access Mariadb as root. It may have something to do with that "Harden ..." that it prompts you to do when you first install. So:
$ sudo su
[sudo] password for user: yourubunturootpassword
# mysql -r root -p
Enter password: yourmariadbrootpassword
and you're in.
The new command to flush the privileges is:
FLUSH PRIVILEGES
The old command FLUSH ALL PRIVILEGES does not work any more.
You will get an error that looks like that:
MariaDB [(none)]> FLUSH ALL PRIVILEGES;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALL PRIVILEGES' at line 1
Hope this helps :)
Run mysql_upgrade.
Check that
SHOW GRANTS FOR 'root'#'localhost';
says
GRANT ALL PRIVILEGES ON ... WITH GRANT OPTION
Check that the table exists _mysql.proxies_priv_.
Access denied for user 'root'#'localhost' while attempting to grant privileges. How do I grant privileges?
System like Ubuntu prefers to use auth_socket plugin. It will try to authenticate by comparing your username in DB and process which makes mysql request; it is described in here
The socket plugin checks whether the socket user name (the operating
system user name) matches the MySQL user name specified by the client
program to the server, and permits the connection only if the names
match.
Instead you may want to back with the mysql_native_password, which will require user/password to authenticate.
About the method to achieve that, I recommend this instead.
First of all close terminal to exit this cmd;
Then run: sudo mysql (Do not forgot sudo otherwise MySQL requires password which you don't know - use sudo to skip mysql password authentication)
Select mysql database by following cmd
mysql> use mysql
then set new password for your root user
mysql> alter user root#localhost identified with mysql_native_password by 'MyNewPassword#123';
then
mysql> flush privileges;
and quit to close mysql connection
mysql> quit
Now you can run sudo mysql_secure_installation without any error
Here you have to enter your new set password (MyNewPassword#123)
I am Using Ubuntu 12.04 (precise) 64-bit Since i am new to Ubuntu i am having issue with grant
I'm using following cmd to grant in Ubuntu example
Sudo mysql -u root -p
create user username identified by 'pass'
grant all privilege on Db.* to username
if check grants using
show grants for username
| GRANT ALL PRIVILEGES ON . TO 'username' IDENTIFIED BY PASSWORD |
now if i login as user-name it will not ask for password it will directly login without password if i enter password for user it gives me error message:
sudo mysql -u username -p
ERROR 1045 (28000): Access denied for user user-name (using password: YES)
and after login if i give
show databases (it will not show any databases which is granted)
please guide me with an example
And also i would like to know tat if stored procedure is created in root how to get access
for other user even tho if i give grant for the particular stored procedure to other user
is there some thing to do with definer
You don't need to run sudo to just connect to MySQL as root.
Create a database:
$ mysqladmin create mydatabase -uroot -p
Connect as root or another user who can grant permissions
$ mysql -D mydatabase -uroot -p
Allow foouser to connect from this very same machine
mysql> grant all privileges on mydatabase.* to 'foouser'#'localhost' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
Connect as your new user:
$ mysql -D mydatabase -u foouser -p
Enter password: pass
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 129
Server version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)
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
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.
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
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';
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
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`;
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.
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.
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
connect mysql with sudo & gives permission for the necessary user using,
sudo mysql -u user;
GRANT ALL PRIVILEGES ON database_name.* TO 'user'#'localhost';
#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:
Hope this helps!
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
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!
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';