Mysql error 1064 while changing password - mysql

While trying to change mysql password by following steps given in this solution, i got the following error-
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('letsrock') WHERE User = 'root' AND Host = 'localhost';
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 '('letsrock') WHERE User = 'root' AND Host = 'localhost'' at line 1
Is there a new syntax for update command in mysql or what? I am using 8.0.12 version.

Both 'user' and 'host' are reserved words and cannot be used as identifiers without enclosing them in backticks. Like so:
UPDATE mysql.user
SET authentication_string = PASSWORD('letsrock')
WHERE `User` = 'root' AND `Host` = 'localhost';
See: https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Related

Unable to update root Mysql Password

I am unable reset mysql password using below query:
UPDATE mysql.user
SET authentication_string=PASSWORD('root')
WHERE user='root'
and host='localhost';
i am getting below 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 '('root') WHERE User = 'root' AND Host = 'localhost'' at line 1
As #spencer7593 points out, the PASSWORD() function was removed in MySQL 8.0 so this code won't work any longer even if it did in previous versions. The syntax error arises because, not recognizing that as a function, the ( character following it is unexpected and a syntax error.
This is because in addition to (previously) being a function it still a keyword.
The way you should be adjusting passwords is either through the mysqladmin command-line tool, or via the SET PASSWORD statement:
SET PASSWORD FOR 'root' = '...'

Error 1064 while running PASSWORD function in mysql

I've been busting my ass from last two days to update authentication_string of my root user and mysql is throwing ERROR 1064 at my face. I've tried every option I could (of course except the correct one which I don't know XD. After logging in as ROOT, I tried this code.
mysql> UPDATE MYSQL.USER SET AUTHENTICATION_STRING = PASSWORD('OJ') WHERE `USER` = 'ROOT';
And I'm getting 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 '('OJ') WHERE `USER` = 'ROOT'' at line 1
I can't figure out what's going wrong here.
Thank you.

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')";

SQL syntax error at simple statement

I'm trying to execute
ALTER USER 'root'#'localhost' IDENTIFIED BY 'my_new_password';
but getting
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 'IDENTIFIED BY 'my_new_password'' at line 1
What am i doing wrong?
The way you want to change the password is only valid since MySQL 5.7.6. You can use the SET PASSWORD statement for older versions instead:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('my_new_password');
The ALTER USER statement is available since MySQL 5.7.6. Make sure you are using this version or a newer one to use your statement (thx #Dez).

SQL database with column in the tablename

I have an error in my MySQL statement (using shell file), which I can't seem to resolve. Tried to put quotes etc but no luck. When I created it using phpmyadmin it works, but I want to have it scripted. Must be something small... Any ideas?
--- Create mySQL database ---
CREATE DATABASE IF NOT EXISTS _test-123;
CREATE USER 'test-123'#localhost IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON _test-123.* TO 'test-123'#'localhost';
FLUSH PRIVILEGES;
Enter password:
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 '-123' at line 1
Try removing the "-" and do this one instead-
CREATE DATABASE IF NOT EXISTS _test_123;