My connection string for MySQL is:
"Server=localhost;User ID=root;Password=123;pooling=yes;charset=utf8;DataBase=.;"
My questions are :
What query should I write to get database names that exist?
What query should I write to get server version?
I have error because of my connection string ends with DataBase=.
What should I write instead of the dot?
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
SELECT VARIABLE_NAME, VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'VERSION'
Use INFORMATION_SCHEMA as the database.
To get the list of databases, you can use SHOW DATABASES:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
To get the version number of your MySQL Server, you can use SELECT VERSION():
SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.1.45 |
+-----------+
1 row in set (0.01 sec)
As for the question about the connection string, you'd want to put a database name instead of the dot, such as Database=test.
show Databases;
Will return you all the registered databases.
And
show variables;
will return a bunch of name value pairs, one of which is the version number.
Related
I want to find all tables and views created before a certain timestamp. For tables it is easy, just
SELECT * FROM information_schema.tables
WHERE table_type = 'TABLE' AND create_time < :ts
But for views it is not so simple. The create_time column is null for all the views in information_chema.tables. e.g.
MariaDB [MYDB]> SELECT IF(create_time IS NULL, 'Null', 'Not Null') AS has_create_ts
, COUNT(1)
FROM information_schema.tables
WHERE table_type = 'VIEW' GROUP BY has_create_ts;
+---------------+----------+
| has_create_ts | COUNT(1) |
+---------------+----------+
| Null | 70 |
+---------------+----------+
1 row in set, 10 warnings (0.371 sec)
And the information_schema.views table does not have any timestamp columns.
So how can I find out when a view was created? Or is it just not possible.
If it matters database version is:
MariaDB [MYDB]> SELECT VERSION();
+--------------------+
| VERSION() |
+--------------------+
| 10.3.7-MariaDB-log |
+--------------------+
1 row in set (0.392 sec)
So how can I find out when a view was created? Or is it just not possible.
No, this is not possible.
A view does not actually contains data, it just made of a definition, ie a SQL statement that references data contained in real (physical) tables : hence this is all that INFORMATION_SCHEMA.VIEWS can show you.
How can I enable strict sql_mode in MySQL?
I want to fetch data from SQL and process the same in strict mode.
My current sql_mode is:
mysql> SELECT ##sql_mode;
+------------------------+
| ##sql_mode |
+------------------------+
| NO_ENGINE_SUBSTITUTION |
+------------------------+
You basically have two ways of doing it, using SQL command or changing configuration file. If you set it using SQL command - it will change back after the server is restarted.
Doing it in SQL:
SET GLOBAL sql_mode='STRICT_TRANS_TABLES';
Doing it in config file:
[mysqld]
sql_mode="STRICT_TRANS_TABLES"
File location varies depending on your operating system, more on where to find it here: https://dev.mysql.com/doc/refman/5.7/en/option-files.html
Important to note, that you can have multiple modes specified:
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
this is especially important when using SQL statement, since it could override your whole mode string.
More stuff about SQL modes here: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
Do the following:
SET GLOBAL sql_mode='STRICT_TRANS_TABLES';
The other answers are correct, but they don't work (as-is) for AWS RDS.
If you are running a MySQL server on AWS RDS, then you can't run SET GLOBAL sql_mode='STRICT_TRANS_TABLES'; straightaway because you don't have the requisite permissions, even with admin-level credentials:
mysql> SET GLOBAL sql_mode='STRICT_ALL_TABLES';
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation
In AWS RDS, since it's a managed DB service, you don't have access to the my.cnf or other configuration files directly - so you can't change the settings there either.
However, note that you can set sql_mode at the session-level, but this will be lost across session changes or reboots:
mysql> SET SESSION sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected, 1 warning (0.30 sec)
mysql> SELECT ##session.sql_mode;
+---------------------+
| ##session.sql_mode |
+---------------------+
| STRICT_ALL_TABLES |
+---------------------+
1 row in set (0.31 sec)
So then how do you change sql_mode (or any other parameters for that matter) at a GLOBAL level so that they persist across restarts in AWS RDS MySQL?
You need to create a custom DB Parameter Group in RDS (for example, using the web console) like this:
Then you have to modify your RDS instance and apply the newly-created Parameter Group like so:
Finally, apply your modifications, and reboot (yes, reboot is required) the instance.
And voila, you have your sql_mode set as needed, and it persists across reboots now:
mysql> SELECT ##sql_mode;
+------------------------------------------+
| ##sql_mode |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.69 sec)
mysql> SELECT ##global.sql_mode;
+------------------------------------------+
| ##global.sql_mode |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.62 sec)
mysql> SELECT ##session.sql_mode;
+------------------------------------------+
| ##session.sql_mode |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.38 sec)
When I execute a particular query from mysql prompt,for example
mysql>select version();
the output is as below along with the number of rows and time taken for query execution
+-------------+
| version() |
+-------------+
| 5.5.35 |
+-------------+
1 row in set (0.00 sec)
I need to execute mysql query from the commandline using "-e" option,i.e.when i execute
mysql -u xxx -p*** -e'select version()'
the following output gets displayed
+-------------+
| version() |
+-------------+
| 5.5.35 |
+-------------+
I need to show the output along with the rows and execution time, when query is executed from the commandline.I know that
select row_count();
and
select found_rows();
can be used to get the number of rows affected and number of rows in the table respectively based on the last query executed.
Is there any mysql options or any other way to get the desired output as in the case of former without adding additional commands or queries.
How can i get current timezone in mysql .
May be i'm don't undestand question but...
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
It's the first (and only) column of the first (and only) row of the result of this SELECT query:
SELECT ##global.time_zone;
If you want the session time zone you can use:
SELECT ##session.time_zone;
The result is like this:
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| Europe/Paris |
+--------------------+
1 row in set (0.00 sec)
What command do I use on a mysql command line to see all the databases on some database server that I have permissions to? Specifically I am looking for the DBs that I have full CRUD permissions to.
mysql -e "show databases"
UPDATE:
Based on your edit, here is a query you can run against the mysql database in your server:
mysql> select Db from db where User='aj' and (select_priv='Y' and insert_priv='Y' and update_priv='Y' and delete_priv='Y');
+---------+
| Db |
+---------+
| HopeDB |
| LocusDB |
+---------+
2 rows in set (0.00 sec)