from_base64() not decoding back to original text - mysql

The from_base64() does not decode correctly. Please see the problem demo below.
mysql> select to_base64('sometext');
+-----------------------+
| to_base64('sometext') |
+-----------------------+
| c29tZXRleHQ= |
+-----------------------+
1 row in set (0.27 sec)
mysql> select from_base64('c29tZXRleHQ=');
+----------------------------------------------------------+
| from_base64('c29tZXRleHQ=') |
+----------------------------------------------------------+
| 0x736F6D6574657874 |
+----------------------------------------------------------+
1 row in set (0.00 sec)
This was working till i moved to latest ubuntu 19.10.
Server version: 8.0.19 MySQL Community Server - GPL
mysql --version
mysql Ver 8.0.19-0ubuntu0.19.10.3 for Linux on x86_64 ((Ubuntu))

This is because of the following.
--binary-as-hex
When this option is given, mysql displays binary data using hexadecimal notation (0xvalue). This occurs whether the overall output dislay format is tabular, vertical, HTML, or XML.
As of MySQL 8.0.19, when mysql operates in interactive mode, this option is enabled by default. In addition, output from the status (or \s) command includes this line when the option is enabled implicitly or explicitly:
To disable hexadecimal notation, use --skip-binary-as-hex
MySQL client options

Related

How can I enable strict sql_mode in MySQL?

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)

Mysql st_intersects + st_buffer doesn't work

I tried to test if a line is met in some distance from a point. St_distance just gives me what I want. However, I'm curious about st_intersects + st_buffer:
$ mysql --version
mysql Ver 14.14 Distrib 5.6.24, for Linux (x86_64) using EditLine wrapper
mysql> set #l4=st_geomfromtext('LINESTRING(50 50, 52 45)');
mysql> set #g4=st_geomfromtext('POINT(50 49)');
mysql> select st_distance(#l4, #g4);
--------------
select st_distance(#l4, #g4)
--------------
+-----------------------+
| st_distance(#l4, #g4) |
+-----------------------+
| 0.3713906763541037 |
+-----------------------+
1 row in set (0.00 sec)
I would think the point is very close to the line but obviously MySQL disagrees:
mysql> select st_intersects(st_buffer(#g4, 1), #l4);
--------------
select st_intersects(st_buffer(#g4, 1), #l4)
--------------
+---------------------------------------+
| st_intersects(st_buffer(#g4, 1), #l4) |
+---------------------------------------+
| 0 |
+---------------------------------------+
1 row in set (0.00 sec)
Why? Do I miss something?
P.S.
I have tried the commands above in H2GIS and it says it is indeed true!
ST_Intersects should return true in this case.
This appears to be the bug reported here: https://bugs.mysql.com/bug.php?id=71076, and fixed in MySQL 5.7.6, according to the release notes here: http://forums.mysql.com/read.php?3,629183,629183
"This work also corrected issues that [...], and that
ST_Intersects() sometimes incorrectly calculated the
result for intersections of LineString and Polygon."

How to show all user-defined variables (MySQL)

I set two user-defined variables as shown below but after some time, I forgot the names:
SET #a = 2, #b = 3;
So, does MySQL have the command that displays all user-defined variables?
Starting with MySQL 5.7, the performance schema exposes user variables.
See table performance_schema.user_variables_by_thread
https://dev.mysql.com/doc/refman/5.7/en/performance-schema-user-variable-tables.html
If you have MariaDB (a binary "drop-in" equivalent of MySQL) there is a plugin available, provided by MariaDB itself.
MariaDB 10.2 (equivalent to MySQL 5.7) and above has a plugin that creates a "USER_VARIABLES" table.
Here is how to install the plugin.
Here is an example of its use:
SELECT * FROM information_schema.USER_VARIABLES ORDER BY VARIABLE_NAME;
+---------------+----------------+---------------+--------------------+
| VARIABLE_NAME | VARIABLE_VALUE | VARIABLE_TYPE | CHARACTER_SET_NAME |
+---------------+----------------+---------------+--------------------+
| var | 0 | INT | utf8 |
| var2 | abc | VARCHAR | utf8 |
+---------------+----------------+---------------+--------------------+
MariaDB installs the plugin by default after version MariaDB 10.2.6.
The link above shows how to install it for prior versions.
Double check what version of "mysql" you're running, because sometimes people will refer to a MariaDB as MySQL, due to its use as a "binary drop in replacement" for MySQL. So it's possible that you are running a MariaDB database.
I am not aware of MySQL providing anything similar.
How to check which version of mysql you're running (the prompt is in bold)
From the command line:
$ mysql -v
From the mysql command client:
mysql> SHOW VARIABLES LIKE "%version%";
It is also shown when you first log into the mysql command client, which you can do via:
$ mysql -u your_mysql_username --password=your_mysql_password
With performance_schema.user_variables_by_thread, you can show all user-defined variables as shown below:
mysql> SELECT * FROM performance_schema.user_variables_by_thread;
+-----------+---------------+--------------------------------+
| THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE |
+-----------+---------------+--------------------------------+
| 69 | first_name | 0x4A6F686E |
| 69 | last_name | 0x536D697468 |
+-----------+---------------+--------------------------------+

Show whole text in query result in mysql

I am using mysql (mysql Ver 14.14 Distrib 5.5.34, for debian-linux-gnu (x86_64) using readline 6.2). and i am create a column named external_password
with description external_password | varchar(200) | null =YES | | default =NULL and inserted data.
When I try preform a select operation through command line i got the following result,
mysql> select external_password from login_user WHERE user_name = 'abc#123';
+---------------------------------------------------------+
| external_password |
+---------------------------------------------------------+
| $S$DEjrvXeeDgACuXAN0XkyM6FEPTFcHLcNqV..3SBHxQBpwR9wN7Fd |
+---------------------------------------------------------+
1 row in set (0.00 sec)
But this not showing the full character. between these substring {LcNqV..3SBHxQB} there are lot of other characters are hidden. how can i get the full text by command line??
please help me
NB: i am using using the ubuntu. Also in a situation i can't use phpmyadmin or any other mysql user interfaces.
use ego. this might display more text compared to the table view.
mysql> select external_password from login_user WHERE user_name = 'abc#123'\G
here are other options you can use http://dev.mysql.com/doc/refman/5.1/en/mysql-commands.html

Mysql function TO_SECONDS doesn't exist

I've been passing through this problem for one day, and it's hard to understand why MySql doesn't work easily.
I'm trying to execute the statement below, but it isn't recognized at all.
SELECT TO_SECONDS('2013-09-12 11:15:00');
I get the following error:
ERROR 1305 (42000): FUNCTION db.to_seconds does not exist
I've checked MySQL's documentation and this function is available since version 5.5. So, I updated my previous version and now I'm working with 6.0 (Server version: 6.0.4-alpha-community-log MySQL Community Server (GPL)) but still not working.
mysql> select version();
+---------------------------+
| version() |
+---------------------------+
| 6.0.4-alpha-community-log |
+---------------------------+
1 row in set (0.03 sec)
Anyone knows what is going on?
TO_SECONDS('2013-09-12 11:15:00');
Seconds are a measure of time interval not a time datum - this therefore implies some sort of reference datum - but when there's lots to choose from, which do you want? One solution is to define your own datum:
SELECT TIME_DIFF('2013-09-12 11:15:00', '01-01-2000 00:00:00')
Or use the Unix epoch:
SELECT UNIX_TIMESTAMP('2013-09-12 11:15:00')
I was using MySQL version 6.0 that was installed by AppServ (Apache, MySQL, PHP, phpmyadmin) tool and this version of MySQL hasn't support for TO_SECONDS function. After installing MySQL 5.5 it's working perfectly.
mysql> select to_seconds('2013-09-02 13:33:59');
+-----------------------------------+
| to_seconds('2013-09-02 13:33:59') |
+-----------------------------------+
| 63545348039 |
+-----------------------------------+
1 row in set (0.00 sec)