sql - 当我尝试按照文档更改 root 密码时,出现 SQL 语法错误或密码未更新

标签 sql linux mariadb change-password

我在 Linux 机器上运行 MariaDB MySQL。当我尝试按照文档更改密码时,MySQL 在启动时给我以下错误:

2017-11-17  9:25:47 139640910462912 [Note] Server socket created on IP: '::'.
ERROR: 1064 You have an error in your SQL syntax; check the manual that corresponds
   to your MariaDB server version for the right syntax to use near 
   'USER 'root'@'localhost' IDENTIFIED BY 'pA$sWord^';' at line 1
2017-11-17  9:25:47 139640910462912 [Note] mysqld: ready for connections.
Version: '10.1.28-MariaDB'  socket: '/run/mysqld/mysqld.sock'  port: 3306  MariaDB Server

The specific documentation我正在关注:

The instructions assume that you will start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start mysqld with the --user=mysql option. If you start the server as root without using --user=mysql, the server may create root-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you will need to either change the ownership of the files to mysql or remove them.

Log on to your system as the Unix user that the MySQL server runs as (for example, mysql).

Stop the MySQL server if it is running. Locate the .pid file that contains the server's process ID. The exact location and name of this file depend on your distribution, host name, and configuration. Common locations are /var/lib/mysql/, /var/run/mysqld/, and /usr/local/mysql/data/. Generally, the file name has an extension of .pid and begins with either mysqld or your system's host name.

Stop the MySQL server by sending a normal kill (not kill -9) to the mysqld process. Use the actual path name of the .pid file in the following command:

shell> kill `cat /mysql-data-directory/host_name.pid` 

Use backticks (not forward quotation marks) with the cat command. These cause the output of cat to be substituted into the kill command.

注意:我使用 top 来结束进程。我确定它没有运行。

$ ps aux | grep mysql
user 30201  0.0  0.0  10884  2296 pts/6    S+   09:43   0:00 grep mysql

Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

MySQL 5.7.6 and later:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5 and earlier:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

Save the file. This example assumes that you name the file /home/me/mysql-init. The file contains the password, so do not save it where it can be read by other users. If you are not logged in as mysql (the user the server runs as), make sure that the file has permissions that permit mysql to read it.

Start the MySQL server with the special --init-file option:

shell> mysqld --init-file=/home/me/mysql-init & 

The server executes the contents of the file named by the --init-file option at startup, changing the 'root'@'localhost' account password.

Other options may be necessary as well, depending on how you normally start your server. For example, --defaults-file may be needed before --init-file.

After the server has started successfully, delete /home/me/mysql-init.

MariaDB 版本:

$ sudo pacman -Q | grep mariadb
libmariadbclient 10.1.28-1
mariadb 10.1.28-1
mariadb-clients 10.1.28-1

文件 /change_root_password.txt:

$ ls -lha /change_root_pwd.txt 
-rwxrwxrwx 1 mysql mysql 56 Nov 17 09:24 /change_root_pwd.txt
$ cat /change_root_pwd.txt 
ALTER USER 'root'@'localhost' IDENTIFIED BY 'pA$sWord^';

用于启动 mysqld 的命令导致上述错误:

sudo mysqld --user=mysql --init-file=/change_root_pwd.txt 

I found this documentation使用备用 SQL 查询:

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

当我尝试使用 ALTER USER 查询时,我没有收到任何错误,但密码没有更新。

最佳答案

也许我应该在问这个问题之前了解MariaDB和MySQL的区别。

我不知道为什么使用该方法重置 root 密码不起作用,但 MariaDB 有更简单的方法来更改 root 密码,记录在此处:

https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7

Reset the MariaDB Root Password

If you forget your root MariaDB password, it can be reset.

Stop the current MariaDB server instance, then restart it with an option to not ask for a password:

sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables &

Reconnect to the MariaDB server with the MariaDB root account:

mysql -u root

Use the following commands to reset root’s password. Replace password with a strong password:

use mysql;
update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
flush privileges;
exit

Then restart MariaDB:

sudo systemctl start mariadb

关于sql - 当我尝试按照文档更改 root 密码时,出现 SQL 语法错误或密码未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47354699/

相关文章:

linux - 如何理解目录的 rwx 权限?

PHP MySQL (MariaDB) 查询中断

mysql - 在 Synology DS218play 上,WordPress 无法连接到用户数据库,但 phpmyadmin 可以

mysql - 无法添加外键约束 MySQL Workbench

linux - 使用 BeagleBone Black 读取两个串口时出现 Segmentation Fault 错误

mysql - 分组最大值

linux - 在 linux 上检测调度线程

mysql - 使用 INSERT ... SELECT 防止锁等待超时

mysql - 选择出现次数最多的产品

sql - 从 Android 访问 SQL 数据库?