How to install mysql to run as non-root - mysql

I have tried to install mysql, and then to run with
mysql -u root -p
but I get
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
I can get in with sudo, but I don't want to run as root. How do I get to run mysql as non-root?
I am using Linux Mint 19.1 32-bit.
What I tried:
sudo apt-get install mysql-client
sudo apt-get install mysql-server
sudo /etc/init.d/mysql restart
sudo mysql_secure_installation
Set up VALIDATE PASSWORD? No
Remove anonymous users? No
Disallow root login remotely? Yes
Remove test database and access to it? No
Reload privilege tables now? Yes
Then I try
mysql -u root -p
it asks for the password, I give the same one I gave in mysql_secure_installation...
frank#frank-laptop:~/WebDev$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
Gah.
I have reinstalled mysql client and server several times now, doing a complete uninstall between and getting rid of all databases when asked. I don't get asked for a root password during the install. That seems to be normal for an install in Ubuntu and derivatives, hence the mysql_secure_installation to set the password.
I tried following
http://www.yolinux.com/TUTORIALS/LinuxTutorialMySQL.html
I tried searching on the web for hints, e.g.
https://askubuntu.com/questions/766900/mysql-doesnt-ask-for-root-password-when-installing#766908
and others but I have not been able to get mysql to start without sudo. If I do use sudo I can create a database, add tables and add data so the database is installed, I just need more privilege to run it than I think I should need.
Anywhere I looked, the instructions go from installation to starting mysql without anything special between, so I think I must be doing something quite stupid. But I have not been able to figure out what.

The first time you run mysql -u root -p , you have to run it with sudo in order to create a new user:
DROP USER 'root'#'localhost';
CREATE USER 'XXX'#'%' IDENTIFIED BY 'YYY';
GRANT ALL PRIVILEGES ON *.* TO 'XXX'#'%' WITH GRANT OPTION;
Then you can connect yourself using mysql --user=XXX --password=YYY
I already try but I don't think you can setup your DB without using sudo unfortunately.

Related

Error: Access denied for user 'root'#'localhost' MYSQL UBUNTU 20.04

am trying to deploy a website on a new server I install MYSQL using the command
sudo apt-get install mysql-server
After that I try to access the MYSQL command prompt using the command
sudo mysql -u root -p
it is giving me the error:
Access denied for user 'root'#'localhost' using password(NO).
Any idea on how to solve this? thanks!!
you have first to run
sudo mysql_secure_installation
after that you should have entered a password for root and the server is configured.
You can also use after that
sudo mysql
to get access with administrator rights and change passwords or add new users
The mysql command - which is the MySQL client library - doesn't require root privileges.
You should run it without sudo:
mysql -u root -p.
Then you should be asked to enter the password. As the docs say, the password should be known to you:
For installations using the MySQL APT repository or Debian packages directly downloaded from Oracle, you should have already assigned the root password yourself.

Access denied on mysql#5.7

I installed via brew on macos the package mysql#5.7 so that can work with Laravel, following the present documentation but I cannot access to mysql#5.7
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
the documentation says
Database
If you need a database, try MySQL by running brew install mysql#5.7 on
your command line. Once MySQL has been installed, you may start it
using the brew services start mysql#5.7 command. You can then connect
to the database at 127.0.0.1 using the root username and an empty
string for the password.
I made a long research and found that is a really common problem but whatever thing I tried did not work
I launched mysqld skipping flushing privileges but then cannot root as well with mysql -u root -p
I tried to run a php artisan cache:clear
I tried to use sudo -s to log as root
and other things that at the moment do not remember anymore(basically is 3 days I am busy)
I am really surprised that a framework as Laravel that should be easy, has this kind of problems that are not reported by the documentation tutorial nor laracast, I did not do nothing strange, just followed the guide
I saw a post where for this error was suggested to change password, but also in this case I went through some issue, so I had to combine different responses but now works
I followed mainly this post
but arrived to the point 5:
5 : modify your password your new password should be input in "()"
I had to deal with a change in mysql#5.7 namely if you do show tables you see that there is not a password column anymore, so you get an error message that does not exist the column password,
so I just passed
update user set authentication_string=password('1111') where user='root';// please notice 1111 is the password you fill
as indicated in this other post
then I followed the other step indicated by the post, to flush privileges and quit
finally I started again the daemon with
$brew services start mysql#5.7 and $mysql -u root -p (herethepassword)
did not work so I did
$mysql --password=(herethepassword) --user=root
and worked

After import a mysqldump can't access to MySQL with root or any created user

Background: I use vagrant (Ubuntu 18.04) to create my development environment,I'm install PHP, MySQL and other stuff but recently I got the problem that I can access to mysql and then I got the classic error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: NO)
using sudo mysql or
ERROR 1045 (28000): Access denied for user 'otheruser'#'localhost'
(using password: YES)
when trying mysql -u otheruser -p
The problems is that MySQL is working fine for a fews 'vagrant up' and 'vagrant halt' but for some reason when I start the box again I can't access to MySQL anymore. I know that I can reset the MySQL root password, remove MySQL or do another quick solution, but most of all I want to know what is the cause of the problem because at first I was thinking about something related to an update I done to my vagrant box, or an update on Ubuntu.
When I install mysql on that Box with Ubuntu I use a .sh file to automate the process, here is the section of mysql:
# Mysql Instalation
sudo apt install mysql-server -y
sudo mysql_secure_installation
sudo mysql < setup.sql
and the setup.sql :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpassword';
FLUSH PRIVILEGES;
CREATE USER 'otheruser'#'localhost' IDENTIFIED BY 'otherpassword';
GRANT ALL PRIVILEGES ON * . * TO 'otheruser'#'localhost';
FLUSH PRIVILEGES;
Then I tried to figure out what the problem and tried to resolve it, first I commented the lines of the root statement on the setup.sql file and then later commenting the 'mysql_secure_installation' line on the .sh file but the problems persists; so to solve quickly I destroy the box and then I create it again, but this turned boring at this point, so here I'm here to describe my problem.
so resuming the questions are:
What could be the cause of the problem?, so I can resolve it on future box creations.
What is the best way to solve this kind of problem.
Because it is overwhelming create and destroy the box every time the problem occurs and most important when I move an app to a production server I don't want this kind of problems.
Thanks for your time reading this. Have a nice day.
Update 06/Feb/2020
After review my deploy process to find more information about the problem I find out that the problem starts when I do an import from a mysqldump
from my production server; just after the import and do a vagrant halt and vagrant up again the problem occurs so Im unable to login on MySQL again.
Here is my mysqldump process and my import process to help is there is something wrong.
Production Server with Ubuntu 16.04 MySQL 5.7.29
mysqldump -u root -p --all-databases > alldb_04-02-2020.sql
Vagrant Box with Ubuntu 18.04 MySQL 5.7.29
mysql -u otheruser -p < alldb_04-02-2020.sql

MySQL Workbench on Linux

I installed MySQL on Ubuntu 16.04. I can login to MySQL shell by typing the command:
sudo mysql -u root
However, I also want to see the DB via MySQL Workbench. I installed it on my computer, and when I go to Database -> Connect to Database I get the following window:
When I click 'OK' I get the following dialog:
I checked 1 and 2. 3 I don't know how to check and as for 4, I don't know what is the password at all (I don't have to use it to login via the console).
Do you know how to resolve it?
Firstly never work with root on a server. Period it is a bad habit. So first things first would be to log into the command line and create a user that is the root equivalent and then use that user.
So use the command line and then execute the following steps:
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
This will create an administrative user called username. You can then use this account to log in. Get in the habit of doing this.
Use terminal login mysql
sudo mysql -u root
Initial root password
update user set password=PASSWORD(‘123456’) where User='root';
And then try workbench login again.
If you want to connect mysql service from any others host except localhost,
you need set root host to %
mysql -u root –p
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user; --check result,init value maybe 127.0.0.1 or localhost
I think that I found the problem. When I installed MySQL, I skipped the option to give a password to root user. Therefore, I decided to remove MySQL from my linux by using the command:
apt-get purge mysql mysql-server mysql-common mysql-client
and then re-install it by:
apt-get install mysql-server
This time, I gave a password to root user during the installation, and after the installation had been finished, I opened MySQL Workbench and used the password I gave during the installation.

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