Bash scripting for MySQL in - mysql

good day all,
please i am new to bash scripting and i am having some challenges with my bash script for connecting to a mysql server to reteive information. this is my script:
#! /bin/bash
MYSQL_USER="root"
MYSQL_PASSWORD="bibson13"
MYSQL_DATABASE="fredhosting"
MYSQL="/usr/bin/mysql –u$MYSQL_USER –p$MYSQL_PASSWORD -D$MYSQL_DATABASE"
$MYSQL -ss -e "select name,email,username,password from free_users where id_user=( select max(id_user) from free_users )"
but when i try to run it i keep getting this:
/usr/bin/mysql Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (i686) using readline 6.2
Copyright (c) 2000, 2012, 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.
Usage: /usr/bin/mysql [OPTIONS] [database]
please i really don't know what am doing wrong. i would be most grateful if anyone could help me with some information on what to do, thank you.

The main problem is that you have some broken characters. Specifically, where you meant to type -u and -p (with hyphens), you instead have –u and –p (with en-dashes). This probably results from copying-and-pasting via a word-processor such as Microsoft Word (which is never a good idea). Once you change those en-dashes back to hyphens, you should be O.K.

You have 2 invalid characters into your MYSQL variable (E2 80) before each "-" sign.
Try to cleanup the line.

Related

Cannot access remote MySQL database in shell [duplicate]

I am trying to connect to a remote MySQL server:
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql.exe -u USERNAME -p PASSWORD -h www.example.net DB_NAME
The result:
mysql.exe Ver 14.14 Distrib 5.7.17, for Win64 (x86_64)
Copyright (c) 2000, 2016, 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.
Usage: mysql.exe [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
... and so on, the help page is long.
Why does the mysql.exe utility ignore my commands?
The password must have no space before it:
mysql.exe -u USERNAME -pPASSWORD

MSQL Restore Errof

I am trying to restore an mysql database using
mysql -u root -p databasename < dumpfile.bak
I get this error
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 'mysql Ver 14.14 Distrib 5.7.9, for Win64 (x86_64)
Copyright (c) 2000, 2015, Ora' at line 1
The database is created and is correct looking at it "Show columns from table name" the dumpfile is in the mysql bin folder.
The dump file was created using mysqldump -u root -p databasename > dumpfile.bak
Here are the first few lines of the dumpfile:
mysql Ver 14.14 Distrib 5.7.9, for Win64 (x86_64)
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.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
Can anyone help?
I found the issue, when progman talked about the dbfile, after I posted it, I took another look and found that it was not the correct version of the file. I thought the error was in the query not the file. Thanks for the help!!!!!

mysql command line not outputting results

I'm baffled as to where I'm going wrong with the following mysql command in my bash script to get a SELECT statement out in HTML format.
#!/bin/bash
dbhost="localhost"
dbname="unicentaopos"
dbuser="user"
dbpass="pass"
mysql -h $dbhost -u $dbuser -p $dbpass -H -e "SELECT NOW();" $dbname > /home/imperial/.scripts/test.txt
Everything is identical to my script, except for the credentials for obvious reasons. I have also tried hard coding the vars into the command - same results.
I cannot see anything wrong with what I've done, but the text file produced just contains mysql usage options:
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
Copyright (c) 2000, 2019, 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.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
etc etc etc etc.....
Can anyone tell me what's wrong?
is the dbhost variable accessed correctly?
#dbhost -> $dbhost

Git Bash 2.5 cannot connect to mysql

I'm having a really annoying problem to connect to MySQL by Git Bash 2.5 (via Windows on localhost).
Everything works fine trough CMD and Mysysgit.
$ mysql -u root -proot
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 7
Server version: 5.6.26-log MySQL Community Server (GPL)
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>
But, look at this. Git SCM for windows still stuck, blinking the cursor for ever and ever. Have you guys seen this? Anyone can help me?
$ mysql -u root -proot
Warning: Using a password on the command line interface can be insecure.
Thanks in advance.
the solution is provided here:
Git Bash mysql blank
use this winpty before any window command and it works.
winpty mysql -u root -proot
The issue 242 point out:
this is a known problem (see git-for-windows/build-extra/installer):
Some console programs interact correctly with MinTTY only when called through winpty (e.g. the Python console needs to be started as winpty python instead of just python).
If you do not like that, feel free to choose the installer option not to use MinTTY.
Please note also that Git for Windows is not your "Linux on Windows". It really is supporting Git on Windows and not necessarily mysql.
Using mysql in a docker container (like nkratzke/EasyMySQL) would be easier and provide a more manageable Linux environment.

Weird output when using mysql via command line

I am trying to access mysql via command line, but when I enter the command to access my DB, I get the mysql --help text as an output. Here is what I am typing in:
mysql -h localhost -u USER -p PASSWORD DB_NAME
I have confirmed all of my credentials are correct, but when I run that, I get the following:
mysql Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1
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.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One do...
I have also tried running it using php exec. I see the same output that way also. Anyone have any thoughts on why this is occurring?
Remove the space between your -p flag and your actual password, or use --password=PASSWORD. From the man page:
If you use the short option form (-p), you cannot have a space between the option and the password.
Don't include PASSWORD in the command. The "-p" is all you need. It will prompt you for the password without showing the characters you type.