find username,databasename and version in mysql? - mysql

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

Related

MySQL 8.0 "show columns from ..." query returns 0 rows

I am running MySQL 8.0.22 on Mac OS 11.7. Before I upgraded MySQL from 5.6, the following query worked fine.
show columns from Viewing where field="type";
But now, after upgrading to MySQL 8.0.22, I get the expected 1 row returned when I'm logged into MySQL as root:
mysql> show columns from Viewing where field="type";
+-------+------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------------------------+------+-----+---------+-------+
| type | enum('edits','other','page','preview','whole') | YES | | NULL | |
+-------+------------------------------------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
But when I log in as the user I've created for the webpage that issues this query, 0 rows are returned. This user is view_pjn#localhost, and its grants are:
mysql> show grants for "view_pjn"#"localhost";
+--------------------------------------------------------------------+
| Grants for view_pjn#localhost |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `view_pjn`#`localhost` |
| GRANT SELECT, INSERT ON `blog`.`viewing` TO `view_pjn`#`localhost` |
+--------------------------------------------------------------------+
2 rows in set (0.00 sec)
But when I log into MySQL 8.0.22 as view_pjn, I get the following when I issue this query:
mysql> show columns from Viewing where field="type";
Empty set (0.00 sec)
I have searched MySQL 8.0 Manual and it would seem that as view_pjn#localhost has SELECT, INSERT privileges on blog.viewing, it ought to be able to issue a show columns from Blog.Viewing query.
If anyone can tell me why view_pjn is getting 0 rows returned, I'd really appreciate it.

How do I print a query by MySQL's version in sql file?

I want to print query by MySQL's version in .sql file.
When I use MySQL version 5.6, want to print (2 column)
SELECT user, host FROM mysql.user;
or, I use MySQL version 5.7 or 8, want to print (3 column)
SELECT user, host, account_locked FROM mysql.user;
Because MySQL 5.6 version doesn't have account_locked column.
mysql> SELECT * FROM information_schema.GLOBAL_VARIABLES WHERE variable_name = 'version';
+---------------+-----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+-----------------+
| VERSION | 5.6.22-71.0-log |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'version';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| version | 5.6.22-71.0-log |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql> SELECT ##version;
+-----------------+
| ##version |
+-----------------+
| 5.6.22-71.0-log |
+-----------------+
1 row in set (0.00 sec)
In a Stored Procedure, you can do something like
IF (##version < "5.7") THEN
SELECT user, host FROM mysql.user;
ELSE
SELECT user, host, account_locked FROM mysql.user;
ENDIF;
It gets messier if you also need to handle MariaDB, with its "10.x.y". And, if Percona has any differences, you need to dig deeper into the "version".

MariaDB / MySQL querys are case sensitive?

I just want to know if that querys are case sensitive or not. Are the same these querys?
SELECT * FROM Users WHERE login="john";
SELECT * FROM Users WHERE login="John";
SELECT * FROM Users WHERE login="JOHN";
SELECT * FROM Users WHERE login="jOHn";
I have tried that on my console and all of them worked, but I want to be sure of that if I would use Hibernate or anything else.
Thanks!
According to the MySQL docs,
The default character set and collation are latin1 and
latin1_swedish_ci, so nonbinary string comparisons are case
insensitive by default
As to the second part of you question - this SO answer shows you how to configure to have the searchers be case sensitive.
From MariaDB docs, it depends on OS. For Windows, it's not case-sensitive. For Linux, Database, table, table aliases and trigger names are affected by the systems case-sensitivity, while index, column, column aliases, stored routine and event names are never case sensitive.
Just an example from my Linux ( Debian 10 ) Box in MariaDB (Ver.10.3.18-MariaDB-0+deb10u1 ).May be helpful for someone !!
MariaDB [niffdb]> select * from account_heads where head_desc="Fuel";
+---------+-----------+
| head_id | head_desc |
+---------+-----------+
| 1 | Fuel |
+---------+-----------+
1 row in set (0.004 sec)
MariaDB [niffdb]> select * from account_heads where head_desc="FUEL";
+---------+-----------+
| head_id | head_desc |
+---------+-----------+
| 1 | Fuel |
+---------+-----------+
1 row in set (0.001 sec)
MariaDB [niffdb]> select * from account_heads where head_desc="fUEL";
+---------+-----------+
| head_id | head_desc |
+---------+-----------+
| 1 | Fuel |
+---------+-----------+
1 row in set (0.001 sec)
MariaDB [niffdb]>

Table 'mysql.user' doesn't exist:ERROR

I am creating a db in mysql for a java program.My program works well in my friends system.But I have some problem with my mysql.
The query is below:
mysql> create database sampledb;
Query OK, 1 row affected (0.00 sec)
mysql> use sampledb;
Database changed
mysql> create user zebronics identified by 'zebra123';
ERROR 1146 (42S02): Table 'mysql.user' doesn't exist
I cant create any user for my db.Please help??
My solution was to run
mysql_upgrade -u root
Scenario: I updated the MySQL version on my Mac with 'homebrew upgrade'. Afterwards, some stuff worked, but other commands raised the error described in the question.
Looks like something is messed up with your MySQL installation. The mysql.user table should definitely exist. Try running the command below on your server to create the tables in the database called mysql:
mysql_install_db
If that doesn't work, maybe the permissions on your MySQL data directory are messed up. Look at a "known good" installation as a reference for what the permissions should be.
You could also try re-installing MySQL completely.
Same issue here as lxxxvi describes. Running
mysql_upgrade -u root
allowed me to then successfully enter a password that
mysql_secure_installation
was waiting for.
Your database may be corrupt. Try to check if mysql.user exists:
use mysql;
select * from user;
If these are missing you can try recreating the tables by using
mysql_install_db
or you may have to clean (completely remove it) and reinstall MySQL.
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| datapass_schema |
| mysql |
| test |
+--------------------+
4 rows in set (0.05 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables
-> ;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
23 rows in set (0.00 sec)
mysql> create user m identified by 'm';
Query OK, 0 rows affected (0.02 sec)
check for the database mysql and table user as shown above if that dosent work, your mysql installation is not proper.
use the below command as mention in other post to install tables again
mysql_install_db
You can run the following query to check for the existance of the user table.
SELECT * FROM information_schema.TABLES
WHERE TABLE_NAME LIKE '%user%'
See if you can find a row with the following values in
mysql user BASE TABLE MyISAM
If you cant find this table look at the following link to rebuild the database How to recover/recreate mysql's default 'mysql' database
Try run mysqladmin reload, which is located in /usr/loca/mysql/bin/ on mac.
'Error Code: 1046'.
This error shows that the table does not exist may sometimes be caused by having selected a different database and running a query referring to another table. The results indicates
'No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar'.
Will solve the issue and it worked for me very well.
It sometime happens when you run the grant/ privileges query on an empty database

SELECT ... INTO fails for one variable, but not for two

Running a fairly old version of MySQL:
mysql> SELECT ##version;
+-----------+
| ##version |
+-----------+
| 5.0.77 |
+-----------+
Afraid I'm not at liberty to update it, so I acknowledge the easy answer may be "Upgrade MySQL to a newer version."
When I do SELECT...INTO query which should return a single row with only one column I get an error:
mysql> SELECT id
INTO #active_id
FROM a_table
WHERE active = 1
LIMIT 1
;
> Undeclared variable: id
But if I add an additional column, it works fine:
mysql> SELECT id, 42
INTO #active_id, #ignored
FROM a_table
WHERE active = 1
LIMIT 1
;
> Query OK, 1 row affected (0.00 sec)
mysql> SELECT #active_id, #ignored;
+------------+----------+
| #active_id | #ignored |
+------------+----------+
| 1 | 42 |
+------------+----------+
Is there a better workaround for this? Is my syntax wrong somehow?