Which context/case/place can we use hexadecimal in mysql query? - mysql

refer to MySQL's official document: https://dev.mysql.com/doc/refman/5.7/en/hexadecimal-literals.html
its official example:
mysql> SELECT X'4D7953514C', CHARSET(X'4D7953514C');
+---------------+------------------------+
| X'4D7953514C' | CHARSET(X'4D7953514C') |
+---------------+------------------------+
| MySQL | binary |
+---------------+------------------------+
However, in my windows-MySQL5.7, I got below: (client is Heidi )
why?

OK I got it.
It's Heidi's problem, not MySQL's.
I changed another client (MySQL-front) , then the text result shown as "MySQL", not 0x4D79...
also I changed to print the query result in a .php page, the text result also shown corrently as "MySQL".

Related

Get the database name in MySQL without using SELECT DATABASE()

I'm trying to do an SQL Injection attack, (This is for an assignment, so I'm not doing anything illegal) and I need to see what the current database name is. However, I'm limited to 15 characters for the input, which is 13 when you factor in escaping the string and commenting out the remainder. SELECT DATABASE() is too long because of this, so is there a way to return the database name in 13 characters or less?
You can use SCHEMA() as it's a synonym for DATABASE(). See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_schema
Use STATUS. It gives you several status variables including the name of the database you are connected to.
https://dev.mysql.com/doc/refman/5.7/en/show-status.html
The command SHOW TABLES is only 11 characters, and the heading of the result set reveals the current database.
Here's an example:
mysql> use test;
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| ... |
+----------------+

MySQL select char(0x542d01 using ?) smile issue

From the command
select char(0x542d01);
I expect
T-1
But MySQL returns
T-:)
From a little of google research I found the result can be modified by specifying the charset in the command, something like
select char(0x542d01 using utf8);
But I wasn't able to find a way to read T-1 from 0x542d01. Would some one give me a hand here, please?
More generally, I think I have a charset issue here.
Function convert can be used to convert data between different charsets.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html
Here is an example of your data:
mysql> select convert(0x542d01 using utf8);
+------------------------------+
| convert(0x542d01 using utf8) |
+------------------------------+
| T- |
+------------------------------+
1 row in set (0.00 sec)

Insert a BLOB value into MySQL database

I have a MYSQL database with a column named img of BLOB type.
When I insert a value into that column like this :
LOAD_FILE('C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Sunset.jpg')
it works !
But like this :
LOAD_FILE('C:/Documents and Settings/Administrator/My Documents/My Pictures/picture.jpg')
it doesn't work and it tells me that the column img cannot be null !
And in both cases the file exists, and I'm connecting to the database as the root user (all privileges), so I don't understand why I'm getting this error.
Thanks in advance
Maybe problem with max_allowed_packet
1.jpg is a small picture and 2.jpg is a big picture
mysql> DESCRIBE blob_files;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file | blob | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> INSERT INTO blob_files(file) VALUE(LOAD_FILE('D:/2.jpg'));
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+--------------------------------------------------------------
------------------+
| Level | Code | Message
|
+---------+------+--------------------------------------------------------------
------------------+
| Warning | 1301 | Result of load_file() was larger than max_allowed_packet (104
8576) - truncated |
+---------+------+--------------------------------------------------------------
------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO blob_files(file) VALUE(LOAD_FILE('D:/1.jpg'));
Query OK, 1 row affected (0.05 sec)
I'm going to post this as an answer, and I'll modify it as needed. In the comments I mentioned mysqld needs to run as administrator. Upon consideration, I realized this is actually not a good idea, since windows UAC is in place for a reason. A better option is to add the necessary permission to the folder.
Go to your My Documents folder under administrator, right click on My Pictures, go to security, and add "LOCAL SERVICE" to your permissions with the read attribute. Then your MySQL server should be able to read from that folder.
If you want to verify LOCAL SERVICE is the proper account, go to start -> run and type services.msc and press enter. Find MySQL, right click and hit properties, go to the Log On tab and see which account it runs as. This is the one you should add to the security tab of the folder with the read permissions.
This may be a little late but how I insert blob files when I just need them as sample data is like this:
Using MysqlWorkbench I enter a record into my blob table without applying the insert.
I right-click the blob field, open value editor then upload an image there.
Now before applying the insert I copy the row and paste it repeatedly (a procedure loop can be used as well)
I do this same thing when updating blob fields as well except in the WHERE clause I would use WHERE primaryKey >= this value this would update all the samples

Mysql binlog path from mysql console

Is there a way to know the bin log files path from mysql console as we can know whether its ON or OFF by using
Select * information_schema.GLOBAL_VARIABLES
where variable_name like '%log_bin%'.
Use this to show on/off:
SHOW VARIABLES LIKE 'log_bin'
Also:
SHOW GLOBAL VARIABLES LIKE '%bin%'
Or
SHOW SESSION VARIABLES LIKE ...
More Information: (notice that some of these values and results changed from 5.5 to 5.6!)
http://dev.mysql.com/doc/refman/5.5/en/show-master-status.html
mysql > SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73 | test | manual,mysql |
+---------------+----------+--------------+------------------+
mysql> SHOW BINARY LOGS;
+---------------+-----------+
| Log_name | File_size |
+---------------+-----------+
| binlog.000015 | 724935 |
| binlog.000016 | 733481 |
+---------------+-----------+
From the docs regarding log-bin"
Setting this option causes the log_bin system variable to be set
to ON (or 1), and not to the base name. This is a known issue;
see Bug #19614 for more information.
There is a workaround using mysqld instead in the Bug 19614 which I modified a bit. If you are scripting, you can use from the mysql client (which I found a bit tedious to do, see the next workaround):
mysql >\! dirname $(mysqld --help --verbose 2> /dev/null | egrep "^log-bin " | grep -o "\/.*")
Looks like there is a patch submitted by Mark Callaghan, and it was never committed. There is a function in WP5465 (which is the work in progress for this patch), however it didn't work properly for me, as the location of the logs can be different across setups.

Unreadable data in Mysql from a GeoDjango PointField

I'm using GeoDjango with MySQL. I use a models.PointField(srid=4326) object, all works fine, the data is correctly saved and retrieved from the database, but when I do a select * from table from MySQL command line, data of PointField field shows unreadable characters, instead of a POINT(x,y) form, as I expected.
Is it a normal behavior?
Yes, that is normal. The data is stored in a binary format.
Try select AsText(geom) from table where 'geom' is the name of your geometry column.
you can use ST_AsText for storing points
mysql> SELECT ST_Y(Point(56.7, 53.34));
+--------------------------+
| ST_Y(Point(56.7, 53.34)) |
+--------------------------+
| 53.34 |
+--------------------------+
mysql> SELECT ST_AsText(ST_Y(Point(56.7, 53.34), 10.5));
+-------------------------------------------+
| ST_AsText(ST_Y(Point(56.7, 53.34), 10.5)) |
+-------------------------------------------+
| POINT(56.7 10.5) |
+-------------------------------------------+
you can refer to the link for more detail:
https://dev.mysql.com/doc/refman/8.0/en/gis-point-property-functions.html