我用以下版本安装了MySQL:
kurt@kurt-ThinkPad:~$ mysql -V
mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
我记得在安装过程中我没有为root用户设置密码。但是,如果我尝试使用mysql
命令简单地启动它,我会收到以下错误:
kurt@kurt-ThinkPad:~$ mysql
ERROR 1045 (28000): Access denied for user 'kurt'@'localhost' (using password: NO)
同样,作为root用户,
kurt@kurt-ThinkPad:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
我尝试按照Resetting the Root Password: Unix and Unix-Like Systems上的说明操作;我在我的主目录中创建了文本文件,并从那里尝试使用--init-file
选项启动MySQL服务器。但是,我再次收到权限错误:
kurt@kurt-ThinkPad:~$ mysqld_safe --init-file=mysql-init &
[1] 18340
kurt@kurt-ThinkPad:~$ /usr/bin/mysqld_safe: 548: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/mysqld_safe.pid: Permission denied
2016-09-20T14:57:04.753720Z mysqld_safe Logging to '/var/log/mysql/error.log'.
cat: /var/run/mysqld/mysqld.pid: Permission denied
rm: cannot remove '/var/run/mysqld/mysqld.pid': Permission denied
2016-09-20T14:57:04.780206Z mysqld_safe Fatal error: Can't remove the pid file:
/var/run/mysqld/mysqld.pid
Please remove it manually and start /usr/bin/mysqld_safe again;
mysqld daemon not started
/usr/bin/mysqld_safe: 135: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
rm: cannot remove '/var/lib/mysql/mysqld_safe.pid': Permission denied
我已经在Cant connect to Mysql server ; Can't create/write the pid file中读到了类似的错误,其中建议的解决方案是将MySQL用户分配到/var/run/mysqld
目录。但是,如果我试试这个,我会得到“不允许操作”:
kurt@kurt-ThinkPad:~$ chown mysql:mysql /var/run/mysqld
chown: changing ownership of '/var/run/mysqld': Operation not permitted
关于如何让MySQL工作的任何想法?
答案 0 :(得分:3)
我注意到一个解决方案是使用mysql
运行sudo
:
kurt@kurt-ThinkPad:~$ sudo mysql
sudo: unable to resolve host kurt-ThinkPad
[sudo] password for kurt:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.15 MySQL Community Server (GPL)
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
(我从https://bbs.archlinux.org/viewtopic.php?id=154010获得了这个建议。)
答案 1 :(得分:2)
选项1 :
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
选项2:(用您拥有的用户名替换YOUR_SYSTEM_USER )
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
请记住,如果使用选项#2,则必须以系统用户名(mysql -u YOUR_SYSTEM_USER)连接到mysql
答案 2 :(得分:1)
默认安装后,我得到了相同的错误代码。我在Ubuntu 18.0.4中运行MariaDB 10.4.7。这是我绕过的方法:
sudo mysql -u root
...
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> set password for 'root'@'localhost'=password('');
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
在此之后,我可以再次以相同的旧方式运行mysql命令:
$ mysql -u root
...
MariaDB [(none)]>
请注意,这将覆盖MariaDB 10.4.7的默认设置的安全性。在测试或开发环境中,完全可以。在生产环境中,您可能需要三思而后行。我建议创建其他用户,然后为其设置密码。
答案 3 :(得分:0)
请注意您的插件。如果它是unix -socket并遵循选项2,则必须知道哪个用户正在使用Shell。因为用户的外壳是唯一的身份验证方式。即使您的shell用户使用其他usename时也使用$ mysql -u root -p,也无法访问数据库。请参见此处的示例https://mariadb.com/kb/en/authentication-plugin-unix-socket/。我建议像option2一样创建一个用户,但要使用插件“ mysql_native_password”。