简体   繁体   English

Ubuntu 18.04 启动 mysql 无root权限

[英]Ubuntu 18.04 start mysql without root privilege

I just installed ubuntu 18.04 and installed LAMP on it, however I had trouble using mysql without the root privilege.我刚刚安装了 ubuntu 18.04 并在其上安装了 LAMP,但是我在没有 root 权限的情况下使用 mysql 时遇到了问题。 when I write:当我写:

mysql -u root -p mysql -u root -p

and enter the password I configured it throws access denied.并输入我配置的密码,它抛出访问被拒绝。 unless if I write除非我写

sudo mysql -u root -p须藤 mysql -u root -p

and enter the password that I'm able to connect.并输入我能够连接的密码。 But I don't want the sudo cause it prevents having workbench to connect to mysql, same applies to phpmyadmin too.但我不想要 sudo,因为它会阻止工作台连接到 mysql,这同样适用于 phpmyadmin。 Any hints on how to fix that?关于如何解决这个问题的任何提示?

It should only be the "-u root" user that requires sudo.它应该只是需要 sudo 的“-u root”用户。 It's good practice to not use root for most access anyways, so just login as root using sudo and create a newuser:无论如何,最好不要将 root 用于大多数访问,因此只需使用 sudo 以 root 身份登录并创建一个新用户:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';

Then you should be able to access "mysql -u newuser" without sudo.然后你应该能够在没有 sudo 的情况下访问“mysql -u newuser”。

I also faced this problem yesterday.我昨天也遇到了这个问题。 To solve this problem follow these steps:要解决此问题,请执行以下步骤:

  1. Connect to MySQL: sudo mysql -u root连接到 MySQL: sudo mysql -u root
  2. Check for mysql users in db: SELECT User,Host FROM mysql.user;检查数据库中的 mysql 用户: SELECT User,Host FROM mysql.user;
  3. Drop root user account: DROP USER 'root'@'localhost';删除 root 用户帐户: DROP USER 'root'@'localhost';
  4. Recreate root user: CREATE USER 'root'@'%' IDENTIFIED BY ''重新创建 root 用户: CREATE USER 'root'@'%' IDENTIFIED BY ''
  5. Grant all privileges to root user: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;授予 root 用户所有权限: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
  6. Set password for root user: ALTER USER 'root'@'%' IDENTIFIED BY 'yourpassword';为 root 用户设置密码: ALTER USER 'root'@'%' IDENTIFIED BY 'yourpassword';

That's it!就是这样! Now you should be able to connect the MySQL.现在您应该能够连接 MySQL。

I am using Ubuntu 18.04 with mysql version 5.7 in workbench but solved this by following these 10 steps我在工作台中使用 Ubuntu 18.04 和 mysql 5.7 版,但按照以下 10 个步骤解决了这个问题

  1. Connect to MySQL: sudo mysql -u root连接到 MySQL: sudo mysql -u root
  2. Check for mysql users in db: SELECT User,Host FROM mysql.user;检查数据库中的 mysql 用户: SELECT User,Host FROM mysql.user;
  3. Drop root user account: DROP USER 'root'@'localhost';删除 root 用户帐户: DROP USER 'root'@'localhost';
  4. SET GLOBAL validate_password_length = 5;
  5. SET GLOBAL validate_password_number_count = 0;
  6. SET GLOBAL validate_password_mixed_case_count = 0;
  7. SET GLOBAL validate_password_special_char_count = 0;
  8. SET GLOBAL validate_password_policy = LOW;
  9. Recreate root user: CREATE USER 'root'@'%' IDENTIFIED BY 'admin'重新创建 root 用户: CREATE USER 'root'@'%' IDENTIFIED BY 'admin'
  10. Grant all privileges to root user: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;授予 root 用户所有权限: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

enter image description here 1. UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';在此处输入图像描述1. UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';

2.FLUSH PRIVILEGES; 2.同花顺特权;

If you couldn't connect to mysql with any mysql user without sudo, try:如果在没有 sudo 的情况下无法使用任何 mysql 用户连接到 mysql,请尝试:

sudo chmod -R 755 /var/lib/mysql/

or或者

sudo chmod -R 755 /var/run/mysql/

then restart the service然后重启服务

sudo services mysql start

or或者

sudo /etc/init.d/mysql start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM