Invalid query: Invalid default value for 'created' in postfixadmin - mysql

I encountered to Invalid " query: Invalid default value for 'created' " in postfixadmin problem and i just want to share my solution with you:
The MySQL "strict mode" is enabled, which doesn't allow the 0000-00-00 default value for date columns.
To work around this problem:
temporarily disable the strict mode in MySQL.
This method allows you to disable the strict mode on your MySQL server by running the following command.
$ mysql -u root -p -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';"
Now, you can verify that the mode is set by running the following:
$ mysql -u root -p -e "SELECT ##GLOBAL.sql_mode;"
and then:
systemctl reload postfixadmin

This mysql query worked for me... after upgrading Ubuntu 16.04 to 18.04:
> root#fast:/etc/mysql# mysql -u root -e "SELECT ##GLOBAL.sql_mode;"
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
root#fast:/etc/mysql# mysql -uroot -e "set global sql_mode = 'ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"
root#fast:/etc/mysql# mysql -u root -e "SELECT ##GLOBAL.sql_mode;"
+------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------+
Drove me nuts trying to find the answer! I'm not sure if it will still work if I restart mysql.

Related

Connecting to MySQL via bash script does not execute queries

I am trying to connect to mysql on localhost via a Bash script.(On a Raspberry PI 3B+ Raspbian)
The connection seems to work but instead of showing the query results it displays some kind of mysql help page.
Which look like this:
mysql Ver 15.1 Distrib 10.1.38-MariaDB, for ... using readline 5.2
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Usage: mysql [OPTIONS] [database]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
...
#!/bin/bash
username="user"
passwort="1234"
mysql -u $username -p$passwort -e "show databases;"
How can i change the command to execute the given statement?
Your password has some characters that was interpreted by the shell , you must protect the password by quoting .
Example my password has a ( space )
MyMac:~ e444$ u="root"
MyMac:~ e444$ p="1234 2"
MyMac:~ e444$ mysql -u$u -p$p -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: YES)
received here a access denied , because the password sent was the 1234
MyMac:~ e444$ mysql -u$u -p"$p" -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
here protect the variable with "

How to run MySQL query in fixed database in MySQL at startup of MySql service in Ubuntu 16.04?

I have to run these query in MySQL everyday in database ABC.
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Is there any way to run these query in MySQL in UBUNTU except runninf from cronjob?
You can directly execute this from MySQL console
mysql -umyuser databasename -p -A
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
If you want to do directly you can add it in mysql configuration file follow the steps here
First, we find out which configuration file our MySQL installation prefers. For that, we need the binary’s location:
$ which mysqld
/usr/sbin/mysqld
Then, we use this path to execute the lookup:
$ /usr/sbin/mysqld --verbose --help | grep -A 1 "Default options"
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
First, we find out the current sql mode:
mysql -u homestead -psecret -e "select ##sql_mode"
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ##sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
We open the configuration file we decided on before (/etc/mysql/my.cnf) and add the following line into the [mysqld] section:
# ... other stuff will probably be here
[mysqld]
sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Save, exit, and restart MySQL:
sudo service mysql restart
The SQL mode is permanently changed and I can continue developing the legacy project until I need some additional strictness.

Supply mysql command in shell script

I am trying to automate some tasks for mysql docker container for that I am using a shell script. But I not able to pass/supply mysql commands after starting mysql server.
Below is my shell script.
#!/bin/bash
mysql.server start
mysql -u root
show databases;
All steps are works as expected else last one 'show databases;' am not getting how to pass 'show databases;' command after mysql server starts. I am on a MAC machine, same behaviour happens on my ubuntu 14.04 container.
below is output on my console.
Please try:
mysql -uUSER -pPASSWORD DBNAME -e 'show databases;'
USER is your user
PASSWORD is your password
-e switch is used to fire commands to mysql via shell
A sample output:
User#Host:~> mysql -uUSER -pPASSWORD DBNAME -e 'show databases ;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mdpdb |
| mdpdb6 |
| mysql |
+--------------------+

Suppress warning output in bash

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
I tried adding 2>/dev/null, &>/dev/null, etc, nothing seemed to suppress the warnings.
mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
The command that is producing the error output to STDERR is the first command, not the second one. Put the STDERR redirection before the pipe, and this should fix your problem.
Better give your exact code attempt and warnings in your original post, but if you try this one :
{ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql; } 2>/dev/null
or
mysql_tzinfo_to_sql 2>/dev/null /usr/share/zoneinfo |
mysql -u root mysql 2>/dev/null
that should work.
Try enclosing it on a subshell
( mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql ) &>/dev/null

Conditional commands in ssh & shell & mysql

I have the following commands in a shell script where I do a mysql dump, then I load that SQL file over ssh into a remote database, and then I update the timestamp.
1. mysqldump -u root files path | gzip -9 > $SQL_FILE
2. cat $SQL_FILE | ssh -i ~/metadata.pem ubuntu#1.2.3.4
"zcat | mysql -u 'root' -h 1.2.3.4 metadata"
3. TIMESTAMP=`date "+%Y-%m-%d-%T"`
4. mysql -u 'root' -h 1.2.3.4 metadata -e "UPDATE path_last_updated SET timestamp=DEFAULT"
Is there any way to improve the above commands. For example, what happens if line 2 fails (for example, due to a connectivity issue), but line 4 succeeds?
How would I make line 4 running conditional on the success of line 2?
You could chain all in one block:
mysqldump -u root files path |
gzip -9 |
ssh -i ~/metadata.pem ubuntu#1.2.3.4 "zcat |\
mysql -u 'root' -h 1.2.3.4 metadata" &&
mysql -u 'root' -h 1.2.3.4 metadata -e "
UPDATE path_last_updated SET timestamp=DEFAULT"
So last mysql command won't be executed if something fail before.
You can use $? for get return code of last command, if it's not 0, it failed.
Or you can use && for example : cmd1 && cmd2.
Or you can use set -e to stop script if occur an error.