mysql command line not outputting results - mysql

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

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

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.

Bash scripting for MySQL in

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.

Grails MySQL import dump

I'm trying to write a function that imports the mysql dump into the database. My code looks like this:
def sout = new StringBuffer()
def serr = new StringBuffer()
String pathToDump = "C:\\Users\\Lojza\\AppData\\Local\\Temp\\protetika-dump-temp-13393134598714336471323836046295.sql"
def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < \"${pathToDump}\" ".execute()
process.consumeProcessOutput(sout, serr)
process.waitForOrKill(10000)
println 'sout: ' + sout // => test text
println 'serr: ' + serr
I tried this code in Grails console, but it returns me this:
mysql Ver 14.14 Distrib 5.5.21, for Win64 (x86)
Copyright (c) 2000, 2011, 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.
...... (and many more lines)
I can't figure out what's wrong with the code above... When I try to execute the generated process line in cmd, it works and did the importing to database. The command I executed looks like this:
mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < "C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"
Thanks for any help!
Regards,
Lojza
Redirection does not work the same way under Java as it does with the Windows command prompt. A way to get around this is to execute the source command with the -e option to read a file. So from the command line you could do
mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 –e “source C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"
Code to do this from groovy (untested),
def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 -e \"source "${pathToDump}\"\" ".execute()