MySQL show current connection info - mysql

I am in a MySQL terminal session but I don't know what server I am connected to, or what database I am connected to.
Is there a MySQL command that will tell me the host, port, and username and database I am using now?

There are MYSQL functions you can use. Like this one that resolves the user:
SELECT USER();
This will return something like root#localhost so you get the host and the user.
To get the current database run this statement:
SELECT DATABASE();
Other useful functions can be found here: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

You can use the status command in MySQL client.
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.8, for Win32 (x86)
Connection id: 1
Current database: test
Current user: ODBC#localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.5.8 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 7 min 16 sec
Threads: 1 Questions: 21 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.48
--------------
mysql>

If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
If you want to know the hostname of your Mysql you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'hostname';
mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| hostname | Dell |
+-------------------+-------+
1 row in set (0.00 sec)
It will give you the hostname for mysql.
If you want to know the username of your Mysql you can use this query on MySQL Command line client --
select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root#localhost |
+----------------+
1 row in set (0.00 sec)
It will give you the username for mysql.

Related

Can a MYSQL trigger access the name of connection's user? [duplicate]

I am in a MySQL terminal session but I don't know what server I am connected to, or what database I am connected to.
Is there a MySQL command that will tell me the host, port, and username and database I am using now?
There are MYSQL functions you can use. Like this one that resolves the user:
SELECT USER();
This will return something like root#localhost so you get the host and the user.
To get the current database run this statement:
SELECT DATABASE();
Other useful functions can be found here: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html
You can use the status command in MySQL client.
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.8, for Win32 (x86)
Connection id: 1
Current database: test
Current user: ODBC#localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.5.8 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 7 min 16 sec
Threads: 1 Questions: 21 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.48
--------------
mysql>
If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
If you want to know the hostname of your Mysql you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'hostname';
mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| hostname | Dell |
+-------------------+-------+
1 row in set (0.00 sec)
It will give you the hostname for mysql.
If you want to know the username of your Mysql you can use this query on MySQL Command line client --
select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root#localhost |
+----------------+
1 row in set (0.00 sec)
It will give you the username for mysql.

MySQL: check what version : 32 bit or 64 bit?

Can I tell what version (32 bit or 64 bit) of MySQL I am running by using Terminal?
$ mysql --version
mysql Ver 14.14 Distrib 5.1.45, for apple-darwin10.2.0 (i386) using readline 6.2
$ echo '\s' | mysql
--------------
mysql Ver 14.14 Distrib 5.1.45, for apple-darwin10.2.0 (i386) using readline 6.2
Connection id: 105730
[...]
Server version: 5.1.41 MySQL Community Server (GPL)
[...]
Uptime: 11 days 4 hours 2 min 6 sec
Threads: 2 Questions: 1141270 Slow queries: 0 Opens: 6137 Flush tables: 1 Open tables: 56 Queries per second avg: 1.182
--------------
run this command in command line:
mysql> show variables like 'version_compile_machine';
then you get something like this:
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| version_compile_machine | i386 |
+-------------------------+-------+
1 row in set (0.00 sec)
and then please check this: http://www.redhat.com/archives/rhl-list/2006-October/msg03684.html
you'll see that i386/i686 are 32 bit, and x86_64 is 64 bit.
Hope this helps.
You can use version():
SELECT version();
See more information here :)
Running the command-line MySQL client:
mysql> select version();
OR
mysql> \s
which is an alias for:
mysql> status
You could try the command: (no login needed)
mysql -V
To know your Mysql bit architecture please follow this step.
Open Mysql console from phpmyadmin
Now after entering password type this command
show global variables like 'version_compile_machine';
if version_compile_machine = x86_64 then it is 64 bit
else 32 bit.
I was searching for this also (core dump issues connecting to mysql) and it seemed none of the above answers properly answered the question: e.g. mysql version info doesn't include the build type 32or 64 bit.
found this capttofu: Do I have a 32-bit or 64-bit MySQL? captoflu which uses a simple "file" command to tell what build youre running, in my case.
BensAir:~ Ben$ /usr/local/mysql/bin/mysqld --verbose --help
file /usr/local/mysql/bin/mysqld
/usr/local/mysql/bin/mysqld: Mach-O 64-bit executable x86_64
Get mysql version In Windows with --version parameter:
C:\>C:\xampp\mysql\bin\mysql.exe --V
C:\xampp\mysql\bin\mysql.exe Ver 14.14 Distrib 5.6.11, for Win32 (x86)
Get mysql version In Windows with custom query:
C:\>C:\xampp\mysql\bin\mysql.exe
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.11 |
+-----------+
1 row in set (0.00 sec)
mysql>
Get mysql version in Windows with server variable:
mysql> select ##Version;
+-----------+
| ##Version |
+-----------+
| 5.6.11 |
+-----------+
1 row in set (0.00 sec)
mysql>
Get mysql version in Windows with \s flag.
mysql> \s
--------------
C:\xampp\mysql\bin\mysql.exe Ver 14.14 Distrib 5.6.11, for Win32 (x86)
Connection id: 25
Current database:
Current user: ODBC#localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.6.11 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: cp850
Conn. characterset: cp850
TCP port: 3306
Uptime: 2 hours 48 min 52 sec
Threads: 1 Questions: 169 Slow queries: 0 Opens: 75 Flush tables: 1 Open
tables: 68 Queries per second avg: 0.016
--------------
Use ##version server variable.
select ##version;
This is what I get on my server:
mysql> select ##version;
+-----------------+
| ##version |
+-----------------+
| 5.0.67-0ubuntu6 |
+-----------------+
1 row in set (0.00 sec)
Hope it helps.
Here's a list of all server variables.
No login is needed (OS X 10.11).
$ /usr/local/mysql/bin/mysqld --version

How to get hold of Amazon MySQL RDS certificates

Amazon RDS documentation (http://aws.amazon.com/rds/faqs/#53) specifies that "Amazon RDS generates an SSL certificate for each [MySQL] DB Instance". I haven't been able to find any documentation on how to find the certificates and the certificates are nowhere to be found in the management console.
Where are the certificates?
I found the solution here: https://forums.aws.amazon.com/thread.jspa?threadID=62110.
Download ca cert file from here: https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem
curl -O https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem
Connect to mysql:
mysql -uusername -p --host=host --ssl-ca=mysql-ssl-ca-cert.pem
Check that your connection is really encrypted:
mysql> SHOW STATUS LIKE 'Ssl_cipher';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Ssl_cipher | AES256-SHA |
+---------------+------------+
1 row in set (0.00 sec)
Optionally force SSL for a specific user to connect to MySQL
mysql> ALTER USER 'username'#'host|%' REQUIRE SSL
You can get the AWS RDS certificate file information from the AWS Documentation guide itself
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_MySQL.html
Download the certificate from here
https://rds.amazonaws.com/doc/mysql-ssl-ca-cert.pem
Update - Amazon updated the SSL certificate, you can download the it from here : https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
Use the following command to login into mysql
root#sathish:/usr/src# mysql -h awssathish.xxyyzz.eu-west-1.rds.amazonaws.com -u awssathish -p --ssl-ca=mysql-ssl-ca-cert.pem
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.13-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> GRANT USAGE ON *.* TO ‘awssathish’#’%’ REQUIRE SSL
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> show variables like "%ssl";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_openssl | YES |
| have_ssl | YES |
+---------------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> SHOW STATUS LIKE 'Ssl_cipher';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Ssl_cipher | AES256-SHA |
+---------------+------------+
1 row in set (0.01 sec)
mysql> exit
Bye
Where
awssathish.xxyyzz.eu-west-1.rds.amazonaws.com
is Endpoint of RDS,
awssathish
is the username of the rds server
I used http://aws-blog.io/2016/rds-over-ssl/
You have to get root pem and pem for the region and concatenate 2 files in one.
https://s3.amazonaws.com/rds-downloads/rds-ca-2015-us-west-2.pem
https://s3.amazonaws.com/rds-downloads/rds-ca-2015-root.pem
And merge files to have single rds-ca-2015-us-west-2-bundle.pem file.
With --ssl-ca provide full path to you pem file.

How to kill MySQL connections

I'm building a website with MySQL. I'm using TOAD for MySQL and suddenly I can't connect to the database as I'm getting an error:
"Too many connections"
Is there any way in Toad for MySQL to view existing connections to be able to kill them or simple close all connections all together?
No, there is no built-in MySQL command for that. There are various tools and scripts that support it, you can kill some connections manually or restart the server (but that will be slower).
Use SHOW PROCESSLIST to view all connections, and KILL the process ID's you want to kill.
You could edit the timeout setting to have the MySQL daemon kill the inactive processes itself, or raise the connection count. You can even limit the amount of connections per username, so that if the process keeps misbehaving, the only affected process is the process itself and no other clients on your database get locked out.
If you can't connect yourself anymore to the server, you should know that MySQL always reserves 1 extra connection for a user with the SUPER privilege. Unless your offending process is for some reason using a username with that privilege...
Then after you can access your database again, you should fix the process (website) that's spawning that many connections.
mysql> SHOW PROCESSLIST;
+-----+------+-----------------+------+---------+------+-------+---------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------------+------+---------+------+-------+----------------+
| 143 | root | localhost:61179 | cds | Query | 0 | init | SHOW PROCESSLIST |
| 192 | root | localhost:53793 | cds | Sleep | 4 | | NULL |
+-----+------+-----------------+------+---------+------+-------+----------------+
2 rows in set (0.00 sec)
mysql> KILL 192;
Query OK, 0 rows affected (0.00 sec)
USER 192 :
mysql> SELECT * FROM exept;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> SELECT * FROM exept;
ERROR 2013 (HY000): Lost connection to MySQL server during query
While you can't kill all open connections with a single command, you can create a set of queries to do that for you if there are too many to do by hand.
This example will create a series of KILL <pid>; queries for all some_user's connections from 192.168.1.1 to my_db.
SELECT
CONCAT('KILL ', id, ';')
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE `User` = 'some_user'
AND `Host` = '192.168.1.1'
AND `db` = 'my_db';
I would recommend checking the connections to show the maximum thread connection is
show variables like "max_connections";
sample
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 13 |
+-----------------+-------+
1 row in set
Then increase it by example
set global max_connections = 500;
In MySQL Workbench:
Left-hand side navigator > Management > Client Connections
It gives you the option to kill queries and connections.
Note: this is not TOAD like the OP asked, but MySQL Workbench users like me may end up here
As above mentioned, there is no special command to do it. However, if all those connection are inactive, using 'flush tables;' is able to release all those connection which are not active.

Why does the MySQL built-in function 'current_user()' return a hostname containing a '%' symbol?

Does MySQL have built in function to get host name?
Similar to
select user(); //this returns user#userip
Edit:
select current_user(); //returns user#10.0.3.%
Last symbol is % -- why?
SELECT ##hostname;
--mysql 4.1 didn't have this one.
select current_user(); returns user#10.0.3.% last simbol is % why ??
the % is the record in mysql.user that match your current login
which can be derived from
select concat(user, '#', host) from mysql.user;
the % is determined by host value.
wouldn't his work?
select substring_index(user(),'#', -1) as hostname;
The above is wrong, it returns the user's IP not host's. I was fooled by testing on local. Sorry about that.
I guess this returns host name, but this wouldn't be useful unless you are ready to grep, pipe and cut Just a FYI:
C:\>mysqladmin -u username -pmypassword -h dev.naishelabs.com version
mysqladmin Ver 8.41 Distrib 5.0.22, for Win32 on ia32
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Server version 5.0.77
Protocol version 10
Connection dev.naishelabs.com via TCP/IP
TCP port 3306
Uptime: 73 days 5 hours 7 min 45 sec
If you want the hostname of the database server, you can use SHOW VARIABLES:
mysql> SHOW VARIABLES LIKE 'hostname';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname | munda |
+---------------+-------+
1 row in set (0.00 sec)
It's not a built-in function so it can't be used in a SELECT statement.
Are you looking for CURRENT_USER function.
Returns the user name and host name
combination for the MySQL account that
the server used to authenticate the
current client. This account
determines your access privileges.
The value of CURRENT_USER() can differ
from the value of USER().
You can use user() and current_user() functions. If you want only hostname do something like select substr(current_user(),LOCATE('#', current_user())+1) AS localhost;
You can find details here
The ##hostname variable contains the system hostname:
$ cat /etc/hostname
bruno
$ hostname
bruno
$ mysql
mysql> SELECT ##hostname;
+------------+
| ##hostname |
+------------+
| bruno |
+------------+
Note that this can be combined and used in other queries:
mysql> SELECT name, ##hostname FROM people;
+-------+-------------+
| name | ##hostname |
+-------+-------------+
| Dotan | bruno |
+-------+-------------+
mysql> SELECT CONCAT('I am on server ', ##hostname);
+---------------------------------------+
| CONCAT('I am on server ', ##hostname) |
+---------------------------------------+
| I am on server bruno |
+---------------------------------------+