Connect to mysql server without sudo - mysql

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

Related

Ubuntu 18.04 start mysql without root privilege

I just installed ubuntu 18.04 and installed LAMP on it, however I had trouble using mysql without the root privilege. when I write:
mysql -u root -p
and enter the password I configured it throws access denied. unless if I write
sudo mysql -u root -p
and enter the password that I'm able to connect. But I don't want the sudo cause it prevents having workbench to connect to mysql, same applies to phpmyadmin too. Any hints on how to fix that?
It should only be the "-u root" user that requires sudo. It's good practice to not use root for most access anyways, so just login as root using sudo and create a newuser:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'#'localhost';
Then you should be able to access "mysql -u newuser" without sudo.
I also faced this problem yesterday. To solve this problem follow these steps:
Connect to MySQL: sudo mysql -u root
Check for mysql users in db: SELECT User,Host FROM mysql.user;
Drop root user account: DROP USER 'root'#'localhost';
Recreate root user: CREATE USER 'root'#'%' IDENTIFIED BY ''
Grant all privileges to root user: GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
Set password for root user: ALTER USER 'root'#'%' IDENTIFIED BY 'yourpassword';
That's it! Now you should be able to connect the MySQL.
I am using Ubuntu 18.04 with mysql version 5.7 in workbench but solved this by following these 10 steps
Connect to MySQL: sudo mysql -u root
Check for mysql users in db: SELECT User,Host FROM mysql.user;
Drop root user account: DROP USER 'root'#'localhost';
SET GLOBAL validate_password_length = 5;
SET GLOBAL validate_password_number_count = 0;
SET GLOBAL validate_password_mixed_case_count = 0;
SET GLOBAL validate_password_special_char_count = 0;
SET GLOBAL validate_password_policy = LOW;
Recreate root user: CREATE USER 'root'#'%' IDENTIFIED BY 'admin'
Grant all privileges to root user: GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
enter image description here 1. UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
2.FLUSH PRIVILEGES;
If you couldn't connect to mysql with any mysql user without sudo, try:
sudo chmod -R 755 /var/lib/mysql/
or
sudo chmod -R 755 /var/run/mysql/
then restart the service
sudo services mysql start
or
sudo /etc/init.d/mysql start

can't login to any account in mysql

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

MYSQL Access denied for user 'root'#'localhost'

I did the following steps to use MySQL in Ubuntu:
sudo aptitude install php5-mysql mysql-server
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root mysql
Change root password:
mysql> UPDATE mysql.user SET Password=PASSWORD('SecurePassword') WHERE
User='root';
mysql> FLUSH PRIVILEGES;
mysql> EXIT
Modify /etc/mysql/my.cnf:
[client]
user=root
password=SecurePassword
[mysqld]
...
default-time-zone = '+0:00'
Then:
sudo service mysql start
mysql -u root
mysql> SHOW GRANTS FOR root#localhost
+--------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '[here is the Securepassword]' |
+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
and I receive an error:
Access denied for user 'root'#'localhost' (using password: YES)
It was absolutely correct what you did, but I guess it's not working for one small reason.
You should use identified by password when you are going to grant privileges like this:
mysql> GRANT ALL PRIVILEGES ONE `*`.`*` TO 'root'#'localhost' IDENTIFIED BY PASSWORD
'*A4B6157319038724E3560894F7F932C8886EBFCF' WITH GRANT OPTION;
If you get an error message like this:
Warning: mysql_connect(): Access denied for user: ....
then the problem is that your application can not access the database. This can have several causes:
First of all, make sure the database settings in your db-config.php (connection file for your application) are correct, specifically the name and password of your MySQL user, the name of your database, and the name of your MySQL server.
If you're running your own server, you may need to give your MySQL user proper permissions. Log in to MySQL as the MySQL root user and issue these commands:
GRANT ALL PRIVILEGES ON database_name TO user#host IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
System like Ubuntu prefers to use auth_socket plugin for root account by default. 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 you checking this out instead.

Grant Issue In ubuntu

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)

"mysqld" root access denied

My root user has the following privileges:
mysql> SHOW GRANTS FOR root#'%';
+--------------------------------------------------------------------------------------------------------------+
| Grants for root#% |
+--------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
| GRANT ALL PRIVILEGES ON `bedrock`.* TO 'root'#'%' |
+--------------------------------------------------------------------------------------------------------------+
When I try to execute the following command, I receive the following error message:
mysqladmin root -pSOMEPASSWORD
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: YES)'
Whilst I'm able to execute mysql command normally, I'm not able to do it to mysqld. What's the problem? Thanks.
You may have a user in your database without a password. Start mysql in safe mode then check to ensure none of your users have empty password fields. If you find one, either add a password or delete the user.
It may be that the password is incorrect and needs to be changed
For the improvement of the knowledge base:
The problem was in permissions and the command. It works when calling bin/mysqld_safe --user=mysql rather mysqladmin root -pSOMEPASSWORD. Note that user mysql had already got permissions throught chown -R and chgrp -R.
try this:
$ mysqladmin -u root -p password yourChosenPassword
Enter password:
and re-enter your same chosen password at the prompt, you will then be able to call mysqladmin from the command line:
$ mysqladmin -uroot -pyourChosenPassword [OPTIONS] command command....