MySQL: different evaluation command line and in mysql console - mysql

I was surprised mysql evaluates the same statement differently command line using mysql -e "$query" and in the mysql console when you run mysql and type queries.
I have a table with timestamp field create_date. I run the same command differently and get different results:
In mysql console:
$ mysql -uuser -ppassword database1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33608
Server version: 5.5.32-31.0 Percona Server (GPL), Release rel31.0, Revision 549
Copyright (c) 2009-2013 Percona Ireland Ltd.
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> select DATE_ADD(create_time, INTERVAL DATEDIFF('2014-03-13', "2013-03-13") DAY) as date from table1;
+---------------------+
| date |
+---------------------+
| 2014-03-08 00:00:11 |
| 2014-03-08 00:00:22 |
| 2014-03-10 00:00:33 |
| 2014-03-10 00:00:44 |
| 2014-03-12 00:00:55 |
| 2014-03-12 00:00:32 |
| 2014-03-08 00:00:42 |
| 2014-03-08 00:00:23 |
+---------------------+
8 rows in set (0.00 sec)
The same query from command line using -e parameter gives null results:
$ mysql -uuser -ppassword urbanout_www -e 'select DATE_ADD(create_time, INTERVAL DATEDIFF('2014-03-13', "2013-03-13") DAY) as date from table1;'
+------+
| date |
+------+
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
+------+
Any explanation of this?

When you use single quotes in the query, they're terminating the shell's quote, not being sent to MySQL. Try:
$ mysql -uuser -ppassword urbanout_www -e 'select DATE_ADD(create_time, INTERVAL DATEDIFF("2014-03-13", "2013-03-13") DAY) as date from table1;'

Related

Query returning hex string rather than expected alphanumeric text; RE: mysql8+ default option --binary-as-hex

This works as expected on one machine:
mysql> SELECT `LengthSHOULD` FROM `LengthAndGrowing` WHERE `LenTmpPltzPerKEY`='InCompletedsLengthCheck_0';
+--------------+
| LengthSHOULD |
+--------------+
| A,B,C,D |
+--------------+
but on a different computer the same code is giving me this:
mysql> SELECT `LengthSHOULD` FROM `LengthAndGrowing` WHERE `LenTmpPltzPerKEY`='InCompletedsLengthCheck_0';
+----------------------------+
| LengthSHOULD |
+----------------------------+
| 0x412C422C432C44 |
+----------------------------+
0x41 2C 42 2C 43 2C 44 |||| Hunh : 0x 41 2C 42 2C 43 2C 44
? , , ,
I have gotten this far... asking mysql about what it is using internally:
mysql> SELECT
-> column_name,
-> character_set_name,
-> collation_name
-> FROM information_schema.columns
-> WHERE table_name = 'LengthAndGrowing';
+------------------+--------------------+--------------------+
| COLUMN_NAME | CHARACTER_SET_NAME | COLLATION_NAME |
+------------------+--------------------+--------------------+
| LenTmpPltzPerKEY | utf8mb4 | utf8mb4_0900_ai_ci |
| LengthSHOULD | NULL | NULL |
| GrowingPains | utf8mb4 | utf8mb4_0900_ai_ci |
| AccordianString | NULL | NULL |
| TimePLEASE | utf8mb4 | utf8mb4_0900_ai_ci |
+------------------+--------------------+--------------------+
How should I proceed to unsnarl this?
From the hint in the answer from dcolazin, yep, it was the option
--skip-binary-as-hex.
So apparently mysql 8.x+; things were made such that by default in for instance a bash shell and in the mysql running there, binary data returned by queries just gets splashed on the screen in hex.
I wound up creating under dir ~/ a new .my.cnf file and under group
[mysql] and [mysqld] and for good measure [server-client] adding the
option
skip-binary-as-hex
Useful resource: the SECTION "Option File Syntax" at MySQL Docs:
https://dev.mysql.com/doc/refman/8.0/en/option-files.html#option-file-order
[mysql]
skip-binary-as-hex
[mysqld]
skip-binary-as-hex
[server-client]
skip-binary-as-hex
systemctl restart mysqld
mysql -u thisdelinquentuser -p
and get nice data again.
DBAABA,DBAABB,DBAABC,DBAABD,
DBAACA,DBAACB,DBAACC,DBAACD,
DBAADA,DBAADB,DBAADC,DBAADD,
0x412C422C432C44 is exactly the hexadecimal representation of A,B,C,D and you might try to use the UNHEX() function. Anyway, it is probably the option --binary-as-hex.

Missing databases

When I start my mysql server with usr/sbin/mysqld --skip-grant-tables --user=mysql & (debian 7) and I logged to mysql a show databases query result is this:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| UDA |
| guikuzi |
| hotel_guregas |
| merkaklub |
| mysql |
| performance_schema |
| phpmyadmin |
| superlinea |
| test |
+--------------------+
10 rows in set (0.00 sec)
and this is a info:
mysql> SHOW VARIABLES WHERE Variable_Name LIKE "%dir";
+---------------------------+----------------------------+
| Variable_name | Value |
+---------------------------+----------------------------+
| basedir | /usr |
| character_sets_dir | /usr/share/mysql/charsets/ |
| datadir | /var/lib/mysql/ |
| innodb_data_home_dir | |
| innodb_log_group_home_dir | ./ |
| lc_messages_dir | /usr/share/mysql/ |
| plugin_dir | /usr/lib/mysql/plugin/ |
| slave_load_tmpdir | /tmp |
| tmpdir | /tmp |
+---------------------------+----------------------------+
9 rows in set (0.00 sec)
After that if I stopped this mysql server and start via /etc/init.d/mysql start I have this:
# /etc/init.d/mysql start 3 ↵
[ ok ] Starting mysql (via systemctl): mysql.service.
[root#debian:~]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.5.35-0+wheezy1 (Debian)
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.01 sec)
mysql> SHOW VARIABLES WHERE Variable_Name LIKE "%dir";
+---------------------------+----------------------------+
| Variable_name | Value |
+---------------------------+----------------------------+
| basedir | /usr |
| character_sets_dir | /usr/share/mysql/charsets/ |
| datadir | /var/lib/mysql/ |
| innodb_data_home_dir | |
| innodb_log_group_home_dir | ./ |
| lc_messages_dir | /usr/share/mysql/ |
| plugin_dir | /usr/lib/mysql/plugin/ |
| slave_load_tmpdir | /tmp |
| tmpdir | /tmp |
+---------------------------+----------------------------+
What´s is happening? I started the safe mode because I have to reset root password, but it´s not working either. Now I have my server in safe mode and it is working but I think that is not the best way
Any help or clue?
Thanks in advance
Based on your post, It's quite obvious that in the first instance you started MySQL server without its privilege system turned on (--skip-grant-tables). This option turns off MySQL's internal privilege system, which gives anyone unrestricted access to all databases. So when you connected to the database server through your client, you bypassed any privilege checks and you could see all databases.
--skip-grant-tables This option causes the server to start without using the privilege system at all, which gives anyone with access to
the server unrestricted access to all databases.
"
See related MySQL docs for more info.
In the second instance, you started MySQL normally, the privilege system was turned on which means that connecting users may perform operations that are permitted to them based on their identity.
The MySQL privilege system ensures that all users may perform only the
operations permitted to them. As a user, when you connect to a MySQL
server, your identity is determined by the host from which you connect
and the user name you specify. When you issue requests after
connecting, the system grants privileges according to your identity
and what you want to do.
See more info in the related MySQL docs.
Also, have a look at the answer to this post how to see which user is authenticated to your current MySQL session.
More simple, just type a "\s" and to see basic status information in the MySQL client prompt. For e.g.:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.35-0ubuntu0.13.10.2 (Ubuntu)
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> \s
--------------
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
Connection id: 37
Current database:
Current user: ubuntu#localhost
(...)
I hope the above helps. I tried to show you a basic pointer how to go about troubleshooting such a situation!

How do I program a .sql file in sublime text 2 and then run it within sublime, without going to the Mac Terminal?

So far I've tried this http://www.youtube.com/watch?v=tPd4m3PLVqU but with no luck. Here is what I tried. And I saved it to user packages in sublime text 2, followed by trying to run a select command with command B.
{
"cmd": ["mysql", "-u", "markrios", "-e", "source $file"],
"selector": "source.sql"
}
Also, I apologize in advance if this is a stupid question.
OSX 10.9.1.
Sublime Text 2
Server version: 5.6.15 MySQL Community Server (GPL)
BTW, I'm currently using MySQL Workbench, but I'd like to use Sublime. The syntax highlighting is better within Sublime (in my opinion).
Any help would be appreciated! Thanks!
# MattDMo
Update 1: 2/20/14 5:37 PM PST
after running use cisp; in Sublime I get the following
[Errno 2] No such file or directory
[cmd: [u'mysql', u'-u', u'markrios', u'-e', u'source /Users/markrios/test.html']]
[dir: /Users/markrios]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
[Finished]
I'm also confused about how the sublime text build system knows what password to use for logging into mysql. As I login into the terminal with this command mysql -umarkrios -p and then get this message
` Marks-MacBook-Air-2:~ markrios$ mysql -umarkrios -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.15 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. `
my goal is to get something like this in Sublime Text 2:
`mysql> select Holiday.* from Holiday;
+--------------------+------------+---------------+
| holiday | actualdate | celebratedate |
+--------------------+------------+---------------+
| CHRISTMAS | 2012-12-25 | 2012-12-25 |
| COLUMBUS DAY | 2012-10-08 | 2012-10-08 |
| INDEPENDENCE DAY | 2012-07-04 | 2012-07-04 |
| LABOR DAY | 2012-09-03 | 2012-09-03 |
| MARTIN LUTHER KING | 2012-01-15 | 2012-01-16 |
| MEMORIAL DAY | 2012-05-28 | 2012-05-28 |
| NEW YEAR DAY | 2012-01-01 | 2012-01-02 |
| PRESIDENTS DAY | 2012-02-20 | 2012-02-20 |
| THANKSGIVING | 2012-11-22 | 2012-11-22 |
| VETERANS DAY | 2012-11-11 | 2012-11-12 |
+--------------------+------------+---------------+
10 rows in set (0.01 sec)
mysql> `
Will using this plugin give you the results you need: https://github.com/jum4/sublime-sqlexec? You can load this from package control and I would argue it's much easier than messing with your own build. Hope this helps and Good luck!

see details of a database in terminal ubuntu

I want to see database details such as users that access this database or collation type in mysql, ssh terminal commands.
please help me.
thank you..
You can run mysql console client with --execute parameter for this purpose:
$ mysql -h {hostname} -u {user} -p{password} -e "SHOW PROCESSLIST"
This will produce output similar to the next one:
+----+---------+-----------------+---------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+---------+-----------------+---------+---------+------+-------+------------------+
| 1 | root | {hostname}:3786 | NULL | Query | 0 | NULL | SHOW PROCESSLIST |
+----+---------+-----------------+---------+---------+------+-------+------------------+

mysql show processlist query shows state = null

I have a script running a batch of very similar queries.
All of them, except one, run without any problem.
Only one query is getting stuck.
In "show processlist" the query has state=null
According to docs, show processlist should report "State=null" only for the "show processlist" thread itself.
Server version: 5.0.67 MySQL Community Server (GPL)
mysql> show processlist;
+---------+--------+-----------+--------------+---------+------+-------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+--------+-----------+--------------+---------+------+-------+------------------------------------------------------------------------------------------------------+
| 3866613 | user | localhost | db_name | Query | 1986 | NULL | select log_time,log_action,log_action_id,log_object_id, #abcde:=if(log_action='abcde',to_ |
| 3873414 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+---------+--------+-----------+--------------+---------+------+-------+------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
Could be a bug of your version of mysql, take a look at this bug for more information .