Need to connect database mysql from shell script (linux centos7) - mysql

#!/bin/bash
date +'%F %T'
echo "Show Database"
mysql -u [user] -p[password] -e 'SHOW DATABASES,USE eventime,SELECT * FROM dt;'
output :
2016-09-22 16:01:33
Show Database
Warning: Using a password on the command line interface can be insecure.
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 'USE eventime,SELECT * FROM dt' at line 1
I don't know why not select data
need to connect and select data
and delete data eveytime 90 day ago
thx for help.

Try semicolon instead of comma in command -
mysql -u [user] -p[password] -e 'SHOW DATABASES;USE eventime;SELECT * FROM dt;'

Related

Change mysql root user password using shell script

I am trying to change user password using a shell script as below
#!/bin/bash
mysql.server start
mysql -u root << EOF
SET PASSWORD FOR root#'localhost' = PASSWORD(‘admin’);
EOF
But I am getting below 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 '‘admin’)' at line 1
Instead of SET PASSSWORD command if use something else will work like if I use 'create database databasename' will works.
.
Update1:
If I use "admin" or 'admin' instead of ‘admin’, I got below 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 '“admin2”)' at line 1
.
Update2:
When using -e flag I got below error
./mysql.sh: line 3: syntax error near unexpected token ('
./mysql.sh: line 3:mysql -u root -padmin -e “SET PASSWORD FOR root#'localhost' = PASSWORD(‘admin2’);”'
use mysql -e instead:
#!/bin/bash
mysql.server start
mysql -u root -e "SET PASSWORD FOR root#'localhost' = PASSWORD('admin');"
For anyone trying above and not getting it to work. I ran the below in my terminal and it worked! (Remember to set the password you want ;) )
mysql -u root -e "SET PASSWORD FOR root#localhost = PASSWORD('mypassword')";

Syntax error uploading a .sql file in the Command Line

In the Command Line I created a database with:
create database mysql;
but when i try to upload a database file with the command:
mysql -u root -p database < dbdump.sql;
i receive a syntax error and i don't understand where i'm wrong
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 'mysql
-u root -p database < dbdump.sql' at line 1
confirm you're running the command from a shell and not from mysql.
I recall needing to be in mysql.exe directory when running such commands
remove the spaces around <. Ie, write mysql -u root -p database<dbdump.sql

General server configuration WSO2 EMM

I'm almost through and I got stuck at the General Server Configuration from step 6. As in when I try the 7th step, I receive below error;
mysql> mysql -u admin -p -D EMM_DB <
/usr/local/bin/wso2emm/dbscripts/emm_mysql.sql; 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 'mysql -u admin -p -D EMM_DB <
/usr/local/bin/wso2emm/dbscripts/emm_mysql.sql' at line 1
I assume that the command I'm issuing is incorrect and if someone could clear out the issue.
Database name field, should be joined with the -D and also I would suggest to use UTF8 parameter
mysql> mysql -u admin -p -DEMM_DB --default_character_set=utf8 < /usr/local/bin/wso2emm/dbscripts/emm_mysql.sql;

dumping/restoring single schema from the main big database

I know we can dump and restore the complete database. But, I want to know if I have multiple database or schema in one main database then what will be the syntax for that?
My database name is TEST and I tried following syntax:
### Start MySQL Backup ###
#$MYSQLDUMP -Q -u root -p "database TEST" >/usr/db/backup/"database TEST".sql;
$MYSQLDUMP -uroot "database TEST" >/backup/"database TEST"-$NOW-$(date +"%T").sql;
But, it failed with following error:
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 '$MYSQLDUMP -uroot "database omertest" >/backup/"database omertest"-$NOW-$(date +' at line 1
If you want only schema then use this command to dump:
MYSQLDUMP -u root -p --no-data TEST >/usr/db/backup/db_backup.sql;

Creating, assign user rigths and loading dump into mysql from a perl bash gives mysql error "no database selected"

I have a bash that creates a database, assign user rigths to it and then load a dump. I do this from a bash in opensuse. A parameter with database name is used = $1.
This is the part of my script that handles this:
echo "creating database"
#(This is Line 22)create new database and configure grants
mysql -hlocalhost -uroot -e "create database $1;"
echo "select db, use"
mysql -hlocalhost -uroot -e "use $1;"
echo "granting rights"
mysql -hlocalhost -uroot -e "grant all on $1.* to 'smic_db_root'#'localhost' identified by
'sm_for_all_987';"
echo "loading dump"
#load the database dump
mysql -h localhost -uroot $1</opt/otrsadm/otrs_template.sql
And this is from my error log:
creating database
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 '' at line 1
select db, use
ERROR at line 1: USE must be followed by a database name
granting rights
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 '* to 'smic_db_root'#'localhost' identified by
'sm_for_all_987'' at line 1
loading dump
ERROR 1046 (3D000) at line 22: No database selected
How do I do in order to chose the database in the bash and when do I need to do it. Since the error message is refering to Line 22, I need to do it somewhere inside. I tried adding the use $1; like this:
mysql -hlocalhost -uroot -e "create database $1; **use $1;** grant all on $1.* to 'smic_db_root'#'localhost' identified by 'sm_for_all_987';"
No difference in behaviour.
create/grant commands don't need a database to be selected. They explicitly work at the global level (create) or on the mysql database (grant). Try putting in some debugging to see eactly where the error(s) occuring?
echo "creating database"
mysql .... 'create database ..';
echo "granting rights"
mysql .... 'grant ....';
echo "loading dump"
mysql .... < file.sql
Right now the error messages do not tell you WHAT failed, just that something did. With the echoes added as above, you'll at least know which stage caused which error(s).