Strange behaviour when connecting MySQL with hostname and password - mysql

I've MySQL Server 5.1.73 running on the CentOS 6.x server.
I've set the password for root user.
When I specify the following command it logs me to the mysql shell.
[root#sandbox ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60384
I am checking the hostname
mysql> SELECT ##hostname;
+-------------------------+
| ##hostname |
+-------------------------+
| sandbox.hortonworks.com |
+-------------------------+
Now I loggoff and try to login with specifying host name explicitly and it asks me to provide the password it throws access denied error.
[root#sandbox ~]# hostname -f
sandbox.hortonworks.com
[root#sandbox ~]# mysql -u root -p -h sandbox.hortonworks.com
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'sandbox.hortonworks.com' (using password: YES)
Even stranger part :
I use the same command but remove -p parameter (i.e. using password = false), it logs in successfully without asking password.
[root#sandbox ~]# mysql -u root -h sandbox.hortonworks.com
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 61736
Could any one tell if I am missing any configuration?
Regards,
-JE

Two things:
I expect the first login attempt works but the second doesn't because your root user is only defined as root#localhost - when you try to connect via an external interface (instead of localhost or 127.0.0.1) you can't authenticate as that user. If that's the issue, you'll need to create a root#sandbox.hortonworks.com user account or alter the existing root user.
Secondly, the successful login without password was probably not as successful as you thought. MySQL will "helpfully" log you in as a different user than the one you specified in some situations, see this question: https://superuser.com/questions/266758/how-do-i-see-which-user-i-am-logged-in-as-in-mysql
The way mysql handles users can definitely be confusing (I'm no longer an expert, I've moved on to using PostgreSQL primarily, so I may be missing something in my explanation) but that's my best guess for the behavior that you're seeing.

Related

Unable to log into mysql using password - Ubuntu

I installed mysql in Ubuntu 20.04.
Problem -
Case 1: I have default root user. I can login using sudo mysql but cannot login using mysql -u root -p.
Case 2: I created another user 'local'. I can login using mysql -u local -p and by giving password I set. But problem is if I give wrong password, it still logs me in. I don't know why.
I followed this ERROR 1698 (28000): Access denied for user 'root'#'localhost'
but this didn't help me. I followed other topics too like creating new user for mysql etc.
I cannot access from workbench or from python script too.
Error : ERROR 1698 (28000): Access denied for user 'root'#'localhost'
Login to mysql using sudo mysql -u root
Give your su password and it will login without mysql password.
Run following commands:
USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
exit;
Restart:
sudo systemctl restart mysql.service
After that, run commands to secure MySQL server and create a new root password.
sudo mysql_secure_installation
When prompted, answer the following questions :
New password: Enter password
Re-enter new password:
Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y
Next time, type following command in terminal to login:
sudo mysql -u root -p
Then type mysql password you set recently to sign in.
Good Luck, this worked for after continuous wasting several hours on this thing and following tens of tutorials.
By doing this, I was finally able to establish connection in workbench and in my python scripts.

MySQl log in issue when changing #localhost to IP address

I created a user called 'test1' with all all the privileges, but I changed the host name to be my ip address instead of localhost. I was able to login and create a connection using MySQL WorkBench just fine. However I can not login using terminal because it adds #localhost by default each time I try to login. As you can see the below error, it is adding #'localhost' by default. Am I doing something wrong here? FYi, I am doing all the logging on my local machine and my password is correct since I use using workbench just as fine.
mysql -u 'test1'#'xxx.xxx.x.xx' -p (login command)
ERROR 1045 (28000): Access denied for user 'test1#xxx.xxx.x.xx'#'localhost' (using password: YES)
Here is the correct way of login command when one has a user with host name other than localhost.
mysql -h xxx.xxx.x.xx -u test1 -p

Mysql and phpmyadmin installation in RaspberryPi 3

I am trying to install mysql and phpmyadmin in my raspberrypi 3. I have followed many tutorials on the internet and specifically this link
https://www.youtube.com/watch?v=ozo_npQMQS8
now the problem is that while installing mysql i dont get any window in the command line asking for the password hence i am not able to set the root password..
I am able to get into mysql by "sudo"
sudo mysql -u root
but i am not able to login through the password.
the situation is same for the phpmyadmin. the installation is correct but i am not able to login with the password.
Thanks
So i figure out the problem is with the plugin. so i have to set the plugin to mysql_native_password.
To check the plugin type
USE mysql;
SELECT User, Host, plugin FROM mysql.user;
You may see that mysql_native_password is not there..
so to setup you may write these commands..
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
So now you will be able to login both in phpmyadmin and in console through root password.
You shouldn't need 'sudo' because the -u in mysql -u root specifies the user account with which to connect (and the 'mysql' command should be available for all users, not just root; if not check your path). The reason you're not being prompted for a password is because you haven't told MySQL to use a password when connecting, to do that use -p like mysql -u root -p.
If you didn't set a password during installation, then there shouldn't be a password in which case mysql -u root should have worked for you. Once you've connected, you could set a password (which you probably ought to do for the two user accounts with username root and host '%' and 'localhost' (there may also be one for an IPv6 address). After setting the password, of course you'll need to use it when logging in with the -p flag.
If you're having trouble with phpMyAdmin, show us the error message and relevant part of your configuration file (config.inc.php).

Can a MariaDB account have local and remote access to a database?

I have created a MariaDB database user user. Looks something like:
MariaDB [(none)]> select host,user,password from mysql.user;
+-----------+-------+-------------------------------------------+
| host | user | password |
+-----------+-------+-------------------------------------------+
| % | user | ***************************************** |
+-----------+-------+-------------------------------------------+
I can connect to the database from a remote computer using command like
mysql -uuser -hdevops.ok -p mydatabase
This works as expected. I can login using my password.
When I am on the database server and try to connect with
[vagrant#devops ~]$ mysql -h127.0.0.1 -uuser -p mydatabase
Enter password:
ERROR 1045 (28000): Access denied for user 'user'#'localhost' (using password: YES)
I expected the % to allow access from all nodes in the network including localhost but it seems this is not how it works.
Is it possible to grant this user local access?
I can create another account of course
create user 'myser'#'localhost' identified by '****';
This would allow me access to the database but that feels more like a workaround.
Update
I now think this is only possible by creating two accounts 'myuser'#'localhost' and 'myuser'#'%'
% does not include localhost. Btw 127.0.0.1 and 127.0.0.2 resolve to localhost and as a consequence these are also not matches by %.
Doesn't make sense but it looks like this is the way it is supposed to work.
The % in the host field means any targeted host is possible.
There is one special case, when the database makes a difference between 127.0.0.1 (TCP based connection) and localhost which connects to the local unix socket.
Consider the db settings skip-networking and bind-address. See
https://mariadb.com/kb/en/mariadb/configuring-mariadb-for-remote-client-access/
TCP connections are usually disabled by default - this might be different depending on distribution, package used, ...
So when you type the following it should work:
mysql -hlocalhost -uuser -p mydatabase
References:
https://dev.mysql.com/doc/refman/5.5/en/connecting.html
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.
You could also use the --socket option.

Can't Connect to LocalHost Mysql Server

I recently installed mysql server and client, but I can't connect to the server. I get this message:
Access denied for user 'root'#'localhost' (using password: YES/NO)
Whatever I do i will always get the same message.
I tried commenting bind-address but still the same thing.
If you set up a password for the root user during installation, then start the mysql client with the -p option, mysql -u root -p, and it should prompt you for a password.
If there's no password set, leave the -p off and it should log right in.
If you've somehow forgotten what the root password is, you can fix it by following these instructions... http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html