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')";
Related
I need a one-liner to provision users in my databases. I'm running MySQL 5.6. Here is what I'm setting the password as a variable so that I can pass it dynamically (obviously it won't always be 'password').
mysql_password="password"
mysql -u ted -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD($mysql_password);"
I'm getting the following error when I run this:
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 'password)' at line 1
What is wrong with the statement? Can it be corrected or is another solution available so I accomplish this via Bash?
You need to surround the variable by single quotes:
mysql -u root -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD('$mysql_password');"
#!/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;'
I'm trying to run an update query on a db server via bash command.
I have to update an IP field (which is sorted as a string) but i'm getting a syntax error...
ssh admin#192.168.3.240 "/usr/local/mysql/bin/mysql -D SMARTPARK -u parkuser -ppass -e 'update client SET online=0 where client_ip='192.168.42.11''"
I'm getting as 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 '.42.11' at line 1
which is the error?
Try to escape " character with \" to make sure you don't escape from the string you're sending to your DB.
In other words, try to put the following in the bash file you're executing:
ssh admin#192.168.3.240 "/usr/local/mysql/bin/mysql -D SMARTPARK -u parkuser -ppass -e \"update client SET online=0 where client_ip='192.168.42.11'\""
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
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).