Ubuntu 18.04 start mysql without root privilege - mysql

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

Related

Why Laravel SQLSTATE[HY000] [1698] Access denied for migrate in ubuntu server? [duplicate]

I just installed Ubuntu 16.04 (Xenial Xerus) and installed web server on it. Everything works well, but I cannot access database.
Even if I create new user and grant all privileges, I can't create database
In PHP I'm getting this error:
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost'
When I try to login in terminal, it works, but in PHP and phpMyAdmin don't.
PHP Code:
protected $host = '127.0.0.1';
protected $db = 'dbname';
protected $name = 'root';
protected $pass = 'root';
protected $conn;
private static $settings = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
public function __construct() {
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->db", $this->name, $this->pass, self::$settings);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
It turns out you can't use the root user in 5.7 anymore without becoming a sudo'er. That means you can't just run mysql -u root anymore and have to do sudo mysql -u root instead.
That also means that it will no longer work if you're using the root user in a GUI (or supposedly any non-command line application). To make it work you'll have to create a new user with the required privileges and use that instead.
See this answer for more details.
These steps worked for me on several systems using Ubuntu 16.04 (Xenial Xerus), Apache 2.4, MariaDB, and PDO:
Log into MYSQL as root
mysql -u root
Grant privileges. For a new user, execute:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'#'localhost';
FLUSH PRIVILEGES;
UPDATE for Google Cloud Instances
MySQL on Google Cloud seem to require an alternate command (mind the backticks).
GRANT ALL PRIVILEGES ON `%`.* TO 'newuser'#'localhost';
NOTE:
Depending on wether your new user should be able to grant all privileges to other users as well you could extend the command by the GRANT WITH option. Please be aware that this exposes your user to be sudoer and hence become a higher security risk.
GRANT ALL PRIVILEGES ON `%`.* TO 'newuser'#'localhost' GRANT WITH OPTION;
Bind to all addresses:
The easiest way is to comment out the line in your
/etc/mysql/mariadb.conf.d/50-server.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf file, depending on what system you are running:
#bind-address = 127.0.0.1
Exit MySQL and restart MySQL
exit
service mysql restart
By default it binds only to localhost, but if you comment the line it binds to all interfaces it finds. Commenting out the line is equivalent to bind-address=*.
To check the binding of the MySQL service, execute as root:
netstat -tupan | grep mysql
Use:
sudo mysql -u root
And now in the MySQL client:
use mysql;
update user set plugin='' where User='root';
flush privileges;
\q
Now you should be able to log in as root in phpMyAdmin.
(It was found here.)
To create a user for phpMyAdmin:
sudo mysql -p -u root
Now you can add a new MySQL user with the username of your choice.
CREATE USER 'USERNAME'#'%' IDENTIFIED BY 'PASSWORD';
And finally grant superuser privileges to the user you just created.
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'%' WITH GRANT OPTION;
In short, in MariaDB:
sudo mysql -u root;
use mysql;
UPDATE mysql.user SET plugin = 'mysql_native_password',
Password = PASSWORD('pass1234') WHERE User = 'root';
FLUSH PRIVILEGES;
exit;
ALTER USER or DROP the user and create again works perfectly.
DROP USER root#localhost;
CREATE USER root#localhost IDENTIFIED BY 'root_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';
FLUSH PRIVILEGES;`
MySQL makes a difference between "localhost" and "127.0.0.1".
It might be possible that 'root'#'localhost' is not allowed because there is an entry in the user table that will only allow root login from 127.0.0.1.
This could also explain why some application on your server can connect to the database and some not because there are different ways of connecting to the database. And you currently do not allow it through "localhost".
Just create a new user for MySQL; do not use root. There is a problem with its security issues:
sudo mysql -p -u root
Log in into MySQL or MariaDB with root privileges
CREATE USER 'troy121'#'%' IDENTIFIED BY 'mypassword123';
Log in and create a new user:
GRANT ALL PRIVILEGES ON *.* TO 'magento121121'#'%' WITH GRANT OPTION;
And grant privileges to access "." and "#" "%" any location, not just only 'localhost'.
exit;
If you want to see your privilege table, SHOW GRANTS; and enjoy.
With MySQL client version 14.14 and Distrib 5.7.22, the update statement is now:
update user set authentication_string=password('1111') where user='root';
If you are receiving that error even after creating a new user and assigning them the database privileges, then the one last thing to look at is to check if the users have been assigned the privileges in the database.
To do this, log into to your MySQL client (this is presumably the application that has restricted access to the database, but you as a root can be able to access your database table via mysql -u user -p).
Commands to apply
mysql -u root -p
password: (provide your database credentials)
On successful login, type
use mysql;
from this point, check each user's privileges if it is enabled from the database table as follows:
select User,Grant_priv,Host from db;
If the values of the Grant_priv col for the created user is N, update that value to Y with the following command:
UPDATE db SET Grant_priv = "Y" WHERE User= "your user";
With that, now try accessing the application and making a transaction with the database.
sudo mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
service mysql restart
After restarting mysql server reload the app please.
None from this question reply that solving my problem but i got super easy to solving that problem!
Just open file DEBIAN.CNF :
/etc/mysql/debian.cnf
You will find default sys admin user and pass! login with this account on your PhpMyAdmin then create new user etc whatever you want!
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = 8pTMhYuRMW6jmMG1
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 8pTMhYuRMW6jmMG1
socket = /var/run/mysqld/mysqld.sock
Users for MySQL and for server are two different things. Look how to add a user to the database and log in with these credentials.
I had the same problem in my Ubuntu 20.04 (Focal Fossa) and MySQL 8.0 and I do these steps:
log in to MySQL
sudo mysql -p -u root
Show the users added to MySQL
SELECT user,plugin,host FROM mysql.user
Change the root user plugin from auth_socket to mysql_native_password
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Flush the privileges
FLUSH PRIVILEGES;
Ctrl + z to exit from MySQL
Restart your MySQL service
sudo service MySQL restart
Check your phpMyAdmin page and try to log in.
Use:
sudo mysql -u root
mysql> CREATE USER 'sample'#'localhost' IDENTIFIED BY 'Secure1pass!';
mysql> CREATE DATABASE testdb;
mysql> GRANT ALL PRIVILEGES ON testdb . * TO 'sample'#'localhost';
In case you just want to use your MySQL server on Ubuntu locally and want to connect with your application to a database.
I had 'user'#'%' with all privileges when getting the same error mentioning 'user'#'localhost' denied access.
So I create 'user'#'localhost' with all privileges, and then flush, and even restart services to no avail.
At last I changed $host = '127.0.0.1'; to $host = 'localhost';.
Now it works!

MySQL: Not possible to login to DB without sudo (mysql-server-5.7)

I have installed mysql-server through sudo apt-get install mysql-server.
No when I want to login with mysql -u root -p I get the message ERROR 1698 (28000): Access denied for user 'root'#'localhost'.
But when I login with sudo mysql -u root -p it is possible, everything works fine.
What do I need to fix, that I can login to mysql without sudo command?
That's a permission issue, and working as intended : by default only the root linux user can execute the mysql daemon.
To allow it for other users, throw that command :
sudo chmod -R 755 /var/lib/mysql/
After that you have to restart the mysql daemon. Depending on your linux kernel, try one of these:
/etc/init.d/mysqld restart
or
service mysqld restart
Note that all this is NOT necessary and will lower the security. I see no reason why one would have to execute mysql with a normal user, except maybe on a shared server where several instances of mysql are running under the control of each user.
I was facing the same issue with mysql 5.7
Because normal user not having a priviledges to get the access
..
Open mysql :
sudo mysql -u root -P
Create new user and add all privileges to that user :
replace username and password according to your need. Replace avi and avi123
CREATE USER 'avi'#'localhost' IDENTIFIED BY 'avi123';
GRANT ALL PRIVILEGES ON *.* TO 'avi'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
As stated in this answer: https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7/763359#763359
MySQL 5.7 changed the secure model, root login requires sudo now.
The solution is to create a new user and give them privileges.
The linked answer above solved my problem.
Only root user needs sudo for login . You can create a new user with all privileges and can be logged in without using sudo.
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'#'localhost';
FLUSH PRIVILEGES;
Login:
mysql -u newuser -p

Connect to mysql server without sudo

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

Access denied for user 'root'#'localhost' (using password: YES) after new installation on Ubuntu

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)

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