Supply mysql command in shell script - mysql

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 |
+--------------------+

Related

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

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.

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 "

MySQL Call script from command line, but don't exit the session

I have 15+ Mysql database on different machines and call each of them manually through the command line. I have replaced those commands with an alias and created a windows batch file using DOSKEY
example of running the alias:
c:\Projects>DOSKEY mysql_db1=mysql -u staffing -p staffing -e "select database(), user();"
c:\Projects>mysql_db1
Enter password: ********
+------------+--------------------+
| database() | user() |
+------------+--------------------+
| staffing | staffing#localhost |
+------------+--------------------+
c:\Projects>
This works fine, but is there a way to not exit the MySQL prompt?
Basically, for each database that I log in, I should see that its the correct database and run other commands without exit.
Is it possible with MySQL?

Docker MySQL no output

Docker for Windows
I have a machine set up with 2 containers. They are both running the standard mysql image. One is set up to be the server, and I am linking to it from the other and attempting to run mysql commands.
Ideally, I would like to be able to do this all through the command prompt so I can call it through Python.
I was able to run a few commands error-free and attempted to place my name into the Username table of myDB.
When I run the command:
docker exec sqlserverClient mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"
I get back the output:
Warning: Using a password on the command line interface can be insecure
And nothing else.
Where is my table? Even if the insert went wrong, shouldn't I at least see a blank table?
I expected to see something like this:
+------------+-------------+
| firstname | lastname |
+------------+-------------+
| First | Last |
+------------+-------------+
"docker logs sqlserverClient" doesn't have it. So where did it go?
Try with docker exec -it, to have tty and the output
But remember docker exec is for debug only.
The best practice is to have a docker container with:
an entrypoint set to sqlserverClient
a cmd that you pass in parameter when running a transient container.
That is:
docker run --rm -it sqlimage mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"
Once the command is executed, the container stopped and is rmeoved.
That means you can have an alias like:
alias drs='docker run --rm -it sqlimage'
And call:
drs mysql -uroot -ppassword -e"use myDB" -e"select * from Usernames"

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