Connecting to MySQL via bash script does not execute queries - mysql

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 "

Related

Connect to remote mysql database from within a container

From my local computer, I can easily connect to a remote instance of mysql database using the mysql cli command (assuming environment variables are set):
# mysql -u root -p$DB_PASSWORD -h $DB_HOST --port=$DB_PORT
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7365
Server version: 5.7.14-google (Google)
Copyright (c) 2000, 2017, 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 |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.05 sec)
However, using docker it just hangs:
docker run --rm -it --name mysql -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql \
mysql -u root -p$DB_PASSWORD -h $DB_HOST --port=$DB_PORT
mysql: [Warning] Using a password on the command line interface can be insecure.
show databases;
^C
^C
exit
exit
^C
and have to docker container stop mysql from another terminal window for it to exit the mysql container.
I tried even attaching to the container and run the same command to no avail. Tried connecting to an AWS MySQL instance and Google Cloud SQL instance, also tried to enter variables directly in the command line all with the same result. Why is it hanging and how can I make it work? Thanks!
Try expose the default port for MySQL
docker run -p 3306:3306 ...
I was using Google Cloud SQL and the IP address needed to be whitelisted for it to allow access. So adding that solved the connection issue.
I tried all the ways but below step has resolved my issue.
run command inside container
dpkg-reconfigure tzdata
it will ask you to select for Geographical area and Time zone, then you are done.

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

Database returned an invalid value in QuerySet.datetimes()

I got this error when I try to access the PayPal IPNs from the admin site.
ValueError at /admin/ipn/paypalipn/
Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
Request Method: GET
Request URL: http://myapp.appspot.com/admin/ipn/paypalipn/
Django Version: 1.9
Exception Type: ValueError
Exception Value:
Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
Exception Location: /base/data/home/apps/e~myapp/1.397535426774028716/lib/django/db/models/expressions.py in convert_value, line 945
Python Executable: /base/data/home/runtimes/python27/python27_dist/python
Python Version: 2.7.5
Python Path:
['/base/data/home/apps/e~myapp/1.397535426774028716',
'/base/data/home/apps/e~myapp/1.397535426774028716/lib',
'/base/data/home/runtimes/python27/python27_dist/lib/python27.zip',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/plat-linux2',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-tk',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-old',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-dynload',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/site-packages',
'/base/data/home/runtimes/python27/python27_lib/versions/1',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.5',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/ssl-2.7',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/yaml-3.10']
Server time: Fri, 30 Dec 2016 22:09:03 +0000
I use Google Mysql and I've tried this answer solution1 on Google Cloud Shell.
mysql> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -u root -p
-> "flush tables;"
-> flush tables;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm
ysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -u root -p
"flush tabl' at line 1
mysql>
Actually the solution you Googled should be working, but you wrongly did it.
I'll show them again here, but to clarify, the steps below are all copied from the question description.
Steps:
In your system command line instead of MySQL's command-line interface, type
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -uroot -p, so you will see something like this:
ubuntu#ubuntu$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -uroot -p
(optional) Check if the problem is already solved. If not, enter MySQL command-line by mysql -uroot -p in your system, and then type flush tables;. Your screen will look like this:
ubuntu#ubuntu$ mysql -uroot -p
Enter password:
...
mysql> flush tables;

Can't start mysql (5.7.10) from command line OSX Yosemite (10.10.5)

When I start mysql using System Preferences>MySQL>StartMySQLServer, then I can do this:
/usr/local/mysql-5.7.10-osx10.9-x86_64/bin$ ./mysql -uroot -p
Enter password: <password shown me during installation>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.10
Copyright (c) 2000, 2015, 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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'my new password';
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
And I can see the mysql process created by System Preferences>MySQL>StartMySQLServer:
/Library/LaunchDaemons$ ps -e | grep mysql
1735 ?? 0:00.31 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
1743 ttys000 0:00.00 grep mysql
/Library/LaunchDaemons$
But after stopping the server using System Preferences>MySQL, I am unable to start the mysql server using the command line (following the instructions in the docs):
/usr/local/mysql-5.7.10-osx10.9-x86_64/bin$ sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
/usr/local/mysql-5.7.10-osx10.9-x86_64/bin$ ./mysql -uroot -p
Enter password: <new password>
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
There's no mysql process:
/usr/local/mysql-5.7.10-osx10.9-x86_64/bin$ ps -e | grep mysql
1920 ttys001 0:00.00 grep mysql
/usr/local/mysql-5.7.10-osx10.9-x86_64/bin$
How are you supposed to start the mysql server from the command line?
You have to start the mysql server first before you can connect. I think you you must start mysqld daemon but i am not sure
I use following command on terminal to start mysql:
mysql.server start
Give it a try.
Actually, the answer you want is:
1.first, add your mysql path to ~/.bash_profile:
export PATH=${PATH}:/usr/local/mysql/bin
Save it and open a new terminal.
2.Then, you can start/stop/restart mysql by,(you can also choose to start it from the "mysql" in "system preferences" or to forget it):
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server restart
3.After you start, use command "mysql" at your will~

Import mysql DB with XAMPP in command LINE

I have this command:
mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql
But I get the following error:
Error
unknow command '\U'
unknow command '\m'
unknow command '\D'
unknow command '\L'
For those using a Windows OS, I was able to import a large mysqldump file into my local XAMPP installation using this command in cmd.exe:
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
Also, I just wrote a more detailed answer to another question on MySQL imports, if this is what you're after.
Go to the xampp shell command line
and run
mysql -h localhost -u username databasename < dump.sql
You may also need to specify the host. From your statement, it looks like you run on Windows. Try this:
mysql -h localhost -u #myusername -p #mypasswd MYBASE
< c:/user/folderss/myscript.sql
Or
mysql -h localhost -u #myusername -p #mypasswd MYBASE
< "c:\user\folderss\myscript.sql"
mysql -h localhost -u username databasename < dump.sql
It works, try it.
If password is blank no need to add the -p args.
/opt/lampp/bin/./mysql -u dbusername -p dbname < sqlfilepath.sql
It works:
mysql -u root -p db_name < "C:\folder_name\db_name.sql"
C:\ is for example
Copy Database Dump File at (Windows PC)
D:\xampp\mysql\bin
mysql -h localhost -u root Databasename <#data11.sql
this command posted by Daniel works like charm
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
just put the db username and db name without those backets
**NOTE: Make sure your database file is reside inside the htdocs folder, else u'll get an Access denied error
No Doubt It does the trick:
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
But some time you many find "MySql Server has Gone a way"
For that you need to change mysql/bin/my.ini max_allowed_packet = 64M to my.ini
I think the commands are OK, issue is with the spaces.
Please observe the spaces between -u and #myusername also between -p and #mypasswd.
mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql
Can also be written as:
mysql -u#myusername -p#mypasswd MYBASE < c:\user\folderss\myscript.sql
Also, you need not add -p and the time of command, if your password is blank, it will not ask you for the password.
If you have password to your database, it will ask for the password.
Thanks
The process is simple-
Move the database to the desired folder e.g. /xampp/mysql folder.
Open cmd and navigate to the above location
Use command to import the db - "mysql -u root -p dbpassword newDbName < dbtoimport.sql"
Check for the tables for verification.
In Windows, place the MySQL file (i.e. example.sql) in C:\xampp\mysql\bin.
Open the XAMPP Control Panel, and launch Shell:
cd c:\xampp\mysql\bin
mysql --default-character-set=utf8 -h localhost -u username databasename < example.sql
Note: Using --default-character-set=utf8 can help to prevent encoding issues with special characters if you intend on working with UTF-8.
Afterwards, remove the MySQL file from C:\xampp\mysql\bin.
Do your trouble shooting in controlled steps:
(1) Does the script looks ok?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
type ansi.sql
show databases
(2) Can you connect to your database (even without specified the host)?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51b-community-nt MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
(3) Can you source the script? (Hoping for more/better error info)
mysql> source ansi.sql
+--------------------+
| Database |
+--------------------+
| information_schema |
| ... |
| test |
+--------------------+
7 rows in set (0.01 sec)
mysql> quit
Bye
(4) Why does it (still not) work?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql < ansi.sql
Enter password: ********
Database
information_schema
...
test
I suspected that the encoding of the script could be the culprit, but I got syntax errors for UTF8 or UTF16 encoded files:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '´╗┐
show databases' at line 1
This could be a version thing; so I think you should make sure of the encoding of your script.
I think the OP's problem was not escaping the slashes.
on windows a path like: "c:\user\folderss\myscript.sql" should in the command line be written like either:
c:\\\user\\\folders\\\myscript.sql
or:
c:/user/folders/myscript.sql
therefore "\u" from "c:\user" was interpreted as a command.
see: http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html for more info
here is my try to import database from cmd in xampp server
the correct command for me is this you can change it with your requirements.and i also attached Errors that i face first time importing db from command line.you can see last command that i use and i paste it here also its working fro me.
mysql -h localhost -u root ovxsolut_windhs < C:\Users\eee\Downloads\ovxsolut_windhs.sql
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/filename.sql
mysql -u root -p dbname< "C:\Users\sdkca\Desktop\mydb.sql"
mysql -h localhost -u username -p databasename < dump.sql