Is there a whoami-like function in mysql?
I'm connecting to the mysql command line from a remote host. I'm not sure if my ip address is resolving to a domain name or not. I would like to see something like
mysql> whoami;
+----------------------------------+
| User | Host |
+----------------------------------+
| username | resolved.hostname.com |
+----------------------------------+
1 row in set (0.00 sec)
or
mysql> whoami;
+------------------------+
| User | Host |
+------------------------+
| username | 22.66.88.44 |
+------------------------+
1 row in set (0.00 sec)
You can use the CURRENT_USER and USER functions as follows:
SELECT CURRENT_USER();
SELECT USER();
CURRENT_USER shows who you are authenticated as, while USER shows who you tried to authenticate as.
See the MySQL manual for more information.
If you are using mysql command line utility then try \s command:
mysql> \s
--------------
mysql Ver 14.12 Distrib 5.0.67, for suse-linux-gnu (i686) using readline 5.2
Connection id: 519
Current database:
Current user: admin#localhost
.........
Server version: 5.0.67 SUSE MySQL RPM
.........
Just do a...
select user,host from mysql.user;
Should show you what you want.
Related
I'm trying to use grafana with mysql. MySql is getting data from wordpres. I can connect to the MySql database with grafana but any attempt to query the database results in a permission error quoted below. Does anyone know what I'm doing wrong? I made sure to FLUSH PRIVILEGES; when creating this account.
What account is being used?
mysql> SELECT USER(),CURRENT_USER();
+-------------------+-------------------+
| USER() | CURRENT_USER() |
+-------------------+-------------------+
| grafana#localhost | grafana#localhost |
+-------------------+-------------------+
1 row in set (0.00 sec)
What permissions does the account have?
mysql> show grants for 'grafana'#'localhost';
+------------------------------------------------------------------------+
| Grants for grafana#localhost |
+------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'grafana'#'localhost' WITH GRANT OPTION |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)
Can I select from a table? (Yes)
mysql> select * from wp_users;
+----+---------------+------------------------------------+---------------+---------------------------+----------+---------------------+-----------------------------------------------+-------------+---------------+
| ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name |
+----+---------------+------------------------------------+---------------+---------------------------+----------+---------------------+-----------------------------------------------+-------------+---------------+
| 1 | admin | ********************************** | admin | a.guy#place.com | | 2019-02-22 18:09:58 | | 0 | something |
+----+---------------+------------------------------------+---------------+---------------------------+----------+---------------------+-----------------------------------------------+-------------+---------------+
1 row in set (0.00 sec)
What is my mysql info?
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
Connection id: 42787
Current database: information_schema
Current user: root#localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 43 days 2 hours 29 min 53 sec
Threads: 3 Questions: 3576291 Slow queries: 0 Opens: 1570 Flush tables: 1 Open tables: 692 Queries per second avg: 0.960
--------------
What query am a trying to run in grafana?
SELECT
user_registered,
user_login
FROM wp_users
I tried with with and without a trailing ; - no change.
What does grafana give back to me with this query?
Error 1142: SELECT command denied to user 'grafana'#'localhost' for table 'wp_users'
I realize that this account has too many permissions, but seeing as grafana thinks it doesn't have enough, for now I'm trying to get it to work with at least more than it needs.
After being stumped by this for a few hours, the notion of looking at logs was suggested to me. Since MySql doesn't have logs enabled by default, I changed the settings to enable the logs and restarted MySql. After doing this everything worked fine. It seems all I had to do was to restart the service.
The solution:
sudo service mysql restart
I use mysql -u root -p to login to mysql. Command show databases only shows information_schema database, and current_user is not root
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
2 rows in set (0.00 sec)
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| #localhost |
+----------------+
1 row in set (0.00 sec)
You have no other databases - try to create one.
The current user is probably the anonymous user - by default MySQL ships with the following users, all having empty password (https://dev.mysql.com/doc/refman/5.5/en/default-privileges.html)
Is this the intended behaviour or am I missing something?
-- Abstract: if there's ''#'localhost' user, can login with any username without password,
-- and any users #'%' will be ignored on login.
-------[Root connection]-------
-- All mysql installations I've seen so far have ''#'localhost' user record.
mysql> select host, user from mysql.user;
+-----------+--------+
| host | user |
+-----------+--------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | |
| localhost | root |
+-----------+--------+
4 rows in set (0.00 sec)
-------[Another connection]-------
-- This allows login with ANY username without password from localhost!
$ mysql -u myuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11222
Server version: 5.6.16 MySQL Community Server (GPL)
mysql> quit
Bye
-- Or even without username at all
$ mysql -u '' -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11223
Server version: 5.6.16 MySQL Community Server (GPL)
mysql> quit
Bye
-------[Root connection]-------
-- Let's create a user #'%'
mysql> grant all on mydb.* to 'myuser'#'%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
-------[Another connection]-------
-- This user is ignored
$ mysql -u myuser -p
Enter password: 123123
ERROR 1045 (28000): Access denied for user 'myuser'#'localhost' (using password: YES)
-------[Root connection]-------
-- Let's remove the empty user record
mysql> drop user ''#'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
-- And make sure that we're ok
mysql> select Host, User from mysql.user;
+-----------+------+
| Host | User |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)
-------[Another connection]-------
-- Able to login with the correct user!
$ mysql -u myuser -p
Enter password: 123123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11228
Server version: 5.6.16 MySQL Community Server (GPL)
-- And check the permissions
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> quit
Bye
while using database under MySQL how can i determine my current MySQL version,database name I'm working and logged in username ?
is it possible to get through using query ?
Try this.
mysql> select version(),user(),database();
+-----------+----------------+------------+
| version() | user() | database() |
+-----------+----------------+------------+
| 5.1.41 | root#localhost | bank |
+-----------+----------------+------------+
1 row in set (0.00 sec)
SELECT DATABASE(), USER(), VERSION();
See "Information Functions" in the docs for more details
I've just installed MySQL Community server (5.5.8) on Mac OS X 10.6.6.
I've been following the rules for a secure install (assign password to root, delete anonymous accounts, etc), however, there is one user account which I can't DROP:
mysql> select host, user from mysql.user;
+--------------------------------+------+
| host | user |
+--------------------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| My-Computer-Hostname.local | |
| My-Computer-Hostname.local | root |
| localhost | root |
| localhost | web |
+--------------------------------+------+
6 rows in set (0.00 sec)
mysql> drop user ''#'My-Computer-Hostname.local';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host, user from mysql.user;
+--------------------------------+------+
| host | user |
+--------------------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| My-Computer-Hostname.local | |
| My-Computer-Hostname.local | root |
| localhost | root |
| localhost | web |
+--------------------------------+------+
6 rows in set (0.00 sec)
mysql>
As you can see, MySQL reports no errors when executing the DROP USER command, but doesn't actually delete the user!
I've tried also deleting the user from within phpMyAdmin (3.3.9) and that produced the same results (i.e. reported success, no error messages, user not deleted).
I've researched this and some people suggest that GRANT may be blocking the DROP USER command, however, the user has no GRANT privileges:
mysql> SHOW GRANTS FOR ''#'My-Computer-Hostname.local';
+-----------------------------------------------------------+
| Grants for #my-computer-hostname.local |
+-----------------------------------------------------------+
| GRANT USAGE ON *.* TO ''#'my-computer-hostname.local' |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> REVOKE GRANT OPTION ON *.* FROM ''#'My-Computer-Hostname.local';
ERROR 1141 (42000): There is no such grant defined for user '' on host 'my-computer-hostname.local'
I tried dropping the user again after that but it didn't drop/delete the user either.
I've checked my MySQl error logs and there's nothing unusual in there.
The MySQL manual suggests that it is possible to delete all anonymous accounts, so why can't I delete this one?
Or, to delete just the anonymous one and not the root as well:
mysql> DELETE FROM mysql.user WHERE User='' AND Host='my-computer-hostname.local';
Worked for me on 5.1.57.
This is a known bug due to your uppercase characters: http://bugs.mysql.com/bug.php?id=62255
Use the suggestion from user douger as a workaround
You can still delete the records from the user table:
mysql> DELETE FROM user WHERE host='my-computer-hostname.local';
Query OK, 2 rows affected (0.00 sec)
This method was used prior to MySQL 4.1...
MySQL includes an anonymous user account that allows anyone to connect into the MySQL server without having a user account. This is meant only for testing, and should be removed before the database server is put into a production environment.
Run the following SQL script against the MySQL server to remove the anonymous user account:
DELETE FROM mysql.user WHERE User='';
After making changes to permissions/user accounts, make sure you flush the provilege tables using the following command:
FLUSH PRIVILEGES;