简体   繁体   English

MySQL 8.0如何给root用户所有权限

[英]How to grant all privileges to root user in MySQL 8.0

Tried尝试过

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

Getting获得

ERROR 1064 (42000): You have an error in your SQL syntax; ERROR 1064 (42000): 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'root' WITH GRANT OPTION' at line 1.检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“IDENTIFIED BY 'root' WITH GRANT OPTION'附近使用的正确语法。

Note: The same is working when tried in previous versions.注意:在以前的版本中尝试时同样有效。

Also tried也试过

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

Getting获得

ERROR 1410 (42000): You are not allowed to create a user with GRANT ERROR 1410 (42000):不允许您使用 GRANT 创建用户

MySQL (8.0.11.0) username/password is root/root. MySQL (8.0.11.0) 用户名/密码是root/root。

Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command.从 MySQL 8 开始,您不再可以(隐式)使用GRANT命令创建用户。 Use CREATE USER instead, followed by the GRANT statement:使用CREATE USER代替,后跟GRANT语句:

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Caution about the security risks about WITH GRANT OPTION , see:关于WITH GRANT OPTION的安全风险注意事项,请参见:

I see a lot of (wrong) answers, it is just as simple as this:我看到很多(错误的)答案,就这么简单:

USE mysql;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'P@ssW0rd';
GRANT ALL ON *.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

Note: instead of a self-created user you can use root to connect to the database.注意:您可以使用root连接到数据库,而不是使用自创建user However, using the default root account to let an application connect to the database is not the preferred way.但是,使用默认的 root 帐户让应用程序连接到数据库并不是首选方式。

Alternative privileges (be careful and remember the least-privilege principle):替代特权(小心并记住最小特权原则):

-- Grant user permissions to all tables in my_database from localhost --
GRANT ALL ON my_database.* TO 'user'@'localhost';

-- Grant user permissions to my_table in my_database from localhost --
GRANT ALL ON my_database.my_table TO 'user'@'localhost';

-- Grant user permissions to all tables and databases from all hosts --
GRANT ALL ON *.* TO 'user'@'*';

If you would somehow run into the following error:如果您以某种方式遇到以下错误:

ERROR 1130 (HY000): Host '1.2.3.4' is not allowed to connect to this MySQL server ERROR 1130 (HY000): Host '1.2.3.4' is not allowed to connect to this MySQL server

You need add/change the following two lines in /etc/mysql/my.cnf and restart mysql:您需要在/etc/mysql/my.cnf添加/更改以下两行并重新启动 mysql:

bind-address           = 0.0.0.0
skip-networking

1) This worked for me. 1)这对我有用。 First, create a new user.首先,创建一个新用户。 Example: User foo with password bar示例:用户foo和密码bar

> mysql> CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';

2) Replace the below code with a username with 'foo'. 2) 用带有'foo'的用户名替换下面的代码。

> mysql> GRANT ALL PRIVILEGES ON database_name.* TO'foo'@'localhost';

Note: database_name is the database that you want to have privileges, .注意:database_name 是你想拥有权限的数据库, . means all on all意味着一切

3) Login as user foo 3) 以用户 foo 登录

mysql> mysql -u foo -p

Password: bar密码:bar

4) Make sure your initial connection from Sequelize is set to foo with pw bar. 4) 确保您从 Sequelize 的初始连接设置为 foo 和 pw bar。

For those who've been confused by CREATE USER 'root'@'localhost' when you already have a root account on the server machine, keep in mind that your 'root'@'localhost' and 'root'@'your_remote_ip' are two different users (same user name, yet different scope) in mysql server.对于那些在服务器计算机上已经有 root 帐户的情况下对CREATE USER 'root'@'localhost'感到困惑的人,请记住,您的'root'@'localhost''root'@'your_remote_ip'是mysql 服务器中的两个不同用户(相同的用户名,但范围不同)。 Hence, creating a new user with your_remote_ip postfix will actually create a new valid root user that you can use to access the mysql server from a remote machine.因此,使用your_remote_ip后缀创建新用户实际上将创建一个新的有效root用户,您可以使用该用户从远程机器访问 mysql 服务器。

For example, if you're using root to connect to your mysql server from a remote machine whose IP is 10.154.10.241 and you want to set a password for the remote root account which is 'Abcdef123!@#' , here are steps you would want to follow:例如,如果您使用root从 IP 为10.154.10.241的远程机器连接到您的 mysql 服务器,并且您想为远程 root 帐户设置密码'Abcdef123!@#' ,这里是您的步骤想遵循:

  1. On your mysql server machine, do mysql -u root -p , then enter your password for root to login.在你的 mysql 服务器机器上,执行mysql -u root -p ,然后输入你的密码让root登录。

  2. Once in mysql> session, do this to create root user for the remote scope:进入mysql>会话后,执行此操作为远程范围创建 root 用户:

     mysql> CREATE USER 'root'@'10.154.10.241' IDENTIFIED BY 'Abcdef123!@#';
  3. After the Query OK message, do this to grant the newly created root user all privileges:Query OK消息之后,执行此操作以授予新创建的 root 用户所有权限:

     mysql> GRANT ALL ON *.* TO 'root'@'10.154.10.241';
  4. And then:进而:

     FLUSH PRIVILEGES;
  5. Restart the mysqld service:重启mysqld服务:

     sudo service mysqld restart
  6. Confirm that the server has successfully restarted:确认服务器已成功重启:

     sudo service mysqld status

If the steps above were executed without any error, you can now access to the mysql server from a remote machine using root .如果执行上述步骤没有任何错误,您现在可以使用root从远程机器访问 mysql 服务器。

My Specs:我的规格:

mysql --version
mysql  Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)

What worked for me:什么对我有用:

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'desired_password';
mysql> GRANT ALL PRIVILEGES ON db_name.* TO 'username'@'localhost' WITH GRANT OPTION;

Response in both queries:两个查询中的响应:

Query OK, O rows affected (0.10 sec*)

NB: I created a database (db_name) earlier and was creating a user credential with all privileges granted to all tables in the DB in place of using the default root user which I read somewhere is a best practice.注意:我之前创建了一个数据库 (db_name),并创建了一个用户凭据,该凭据具有授予数据库中所有表的所有权限,而不是使用我在某处读取的默认 root 用户,这是最佳做法。

The specified user just doesn't exist on your MySQL (so, MySQL is trying to create it with GRANT as it did before version 8, but fails with the limitations, introduced in this version).指定的用户在您的 MySQL 中不存在(因此,MySQL 尝试使用 GRANT 创建它,就像它在版本 8 之前所做的那样,但由于此版本中引入的限制而失败)。

MySQL's pretty dumb at this point, so if you have 'root'@'localhost' and trying to grant privileges to 'root'@'%' it treats them as different users, rather than generalized notion for root user on any host, including localhost. MySQL 在这一点上非常愚蠢,所以如果你有 'root'@'localhost' 并试图授予 'root'@'%' 权限,它会将它们视为不同的用户,而不是任何主机上的 root 用户的通用概念,包括本地主机。

The error message is also misleading.错误消息也具有误导性。

So, if you're getting the error message, check your existing users with something like this因此,如果您收到错误消息,请使用以下内容检查现有用户

SELECT CONCAT("'", user, "'@'", host, "'") FROM mysql.user;

and then create missing user (as Mike advised) or adjust your GRANT command to the actual exisiting user specificaion.然后创建缺少的用户(如 Mike 建议的那样)或将您的 GRANT 命令调整为实际存在的用户规范。

Just my 2 cents on the subject.只是我在这个主题上的 2 美分。 I was having the exact same issue with trying to connect from MySQL Workbench.我在尝试从 MySQL Workbench 连接时遇到了完全相同的问题。 I'm running a bitnami-mysql virtual machine to set up a local sandbox for development.我正在运行一个 bitnami-mysql 虚拟机来设置一个本地沙箱进行开发。

Bitnami's tutorial said to run the 'Grant All Privileges' command: Bitnami 的教程说要运行“授予所有权限”命令:

/opt/bitnami/mysql/bin/mysql -u root -p -e "grant all privileges on *.* to 'root'@'%' identified by 'PASSWORD' with grant option";

This was clearly not working, I finally got it to work using Mike Lischke's answer.这显然不起作用,我终于使用 Mike Lischke 的回答让它工作了。

What I think happened was that the root@% user had the wrong credentials associated to it.我认为发生的事情是 root@% 用户的凭据错误。 So if you've tried to modify the user's privileges and with no luck try:因此,如果您尝试修改用户的权限并且没有运气,请尝试:

  1. Dropping the user.删除用户。
  2. Create the user again.再次创建用户。
  3. Make sure you have the correct binding on your my.cnf config file.确保您的 my.cnf 配置文件具有正确的绑定。 In my case I've commented the line out since it's just for a sandbox environment.就我而言,我已注释掉该行,因为它仅适用于沙箱环境。

From Mysql Console:从 Mysql 控制台:

List Users (helpful to see all your users):列出用户(有助于查看所有用户):

select user, host from mysql.user;

Drop Desired User:删除所需用户:

drop user '{{ username }}'@'%';

Create User and Grant Permissions:创建用户并授予权限:

CREATE USER '{{ username }}'@'%' IDENTIFIED BY '{{ password }}';
GRANT ALL PRIVILEGES ON *.* TO '{{ username }}'@'%' WITH GRANT OPTION;

Run this command:运行此命令:

FLUSH PRIVILEGES;

Locate your mysql config file 'my.cnf' and look for a line that looks like this:找到您的 mysql 配置文件“my.cnf”并查找如下所示的行:

bind-address=127.0.0.1

and comment it using a '#':并使用“#”对其进行评论:

#bind-address=127.0.0.1

Then restart your mysql service.然后重启你的mysql服务。

Hope this helps someone having the same issue!希望这可以帮助有同样问题的人!

Copy this and use it at once:复制并立即使用它:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Instead of using single lines of code such as:而不是使用单行代码,例如:

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

Then:然后:

GRANT ALL ON *.* TO 'username'@'localhost';

Then:然后:

FLUSH PRIVILEGES;

Check out your username and domain is the same as created before.检查您的用户名和域是否与之前创建的相同。 Mysql select account by the two colums in user table.If it is different, mysql may think you want to create a new account by grant,which is not supported after 8.0 version. Mysql通过user表中的两列选择账号,如果不同,mysql可能认为你想通过grant创建一个新账号,8.0版本以后不支持了。

in select statement, changing 'user'@'%' to 'user'@'localhost' solved my problem在 select 语句中,将 'user'@'%' 更改为 'user'@'localhost' 解决了我的问题

This worked for me:这对我有用:

mysql> FLUSH PRIVILEGES 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES

My Specs:我的规格:

mysql --version
mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

What worked for me:什么对我有用:

mysql> USE mysql;
mysql> UPDATE User SET Host='%' WHERE User='root' AND Host='localhost';

You will get this error你会得到这个错误

ERROR 1410 (42000): You are not allowed to create a user with GRANT ERROR 1410 (42000): 不允许您创建具有 GRANT 的用户

If you are trying to run a GRANT on a user that doesn't exist!如果您尝试对不存在的用户运行GRANT

Therefore, first run this to make sure the user you use in your GRANT matches exactly to what you have:因此,首先运行它以确保您在GRANT中使用的用户与您拥有的完全匹配:

select User, Host from user;

In particular pay attention whether the user you created is at localhost but the one you are trying to grant to is %特别注意您创建的用户是否在localhost而您尝试授予的用户是%

this commands work for me:这个命令对我有用:

1-login to mysql and see all users 1-登录mysql并查看所有用户

sudo mysql -u root

 select user, host from mysql.user;

2-delete old user 2-删除老用户

drop user root@localhost;

3-create new user 3-创建新用户

CREATE USER 'root'@'localhost' IDENTIFIED BY 'mypassword'

4-add all privileges to it: 4-为其添加所有权限:

GRANT ALL ON *.* TO 'root'@'localhost'

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';

5-finally flush privileges 5-finally 刷新权限

FLUSH PRIVILEGES;

Well, I just had the same problem.好吧,我只是遇到了同样的问题。 Even if route had '%' could not connect remotely.即使路由有 '%' 也无法远程连接。 Now, having a look at my.ini file (config file in windows) the bind-address statement was missed.现在,查看my.ini文件(Windows 中的配置文件), bind-address语句被遗漏了。

So... I putted this bind-address = * after [mysqld] and restarted the service.所以...我把这个bind-address = *放在[mysqld]并重新启动了服务。 Now it works!现在它起作用了!

1. grant privileges 1. 授予权限

mysql> GRANT ALL PRIVILEGES ON . mysql> 授予所有权限。 TO 'root'@'%'WITH GRANT OPTION; 'root'@'%'with GRANT OPTION;

mysql> FLUSH PRIVILEGES mysql> 刷新特权

2. check user table: 2.查看用户表:

mysql> use mysql mysql> 使用 mysql

mysql> select host,user from user mysql> 从用户中选择主机、用户在此处输入图片说明

3.Modify the configuration file 3.修改配置文件

mysql default bind ip:127.0.0.1, if we want to remote visit services,just delete config mysql默认绑定ip:127.0.0.1,如果要远程访问服务,删除config即可

#Modify the configuration file
vi /usr/local/etc/my.cnf

#Comment out the ip-address option
[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1

4.finally restart the services 4.最后重启服务

brew services restart mysql brew 服务重启 mysql

Try this, i had the same issue and i tried few options, but the below worked.试试这个,我遇到了同样的问题,我尝试了几个选项,但下面的方法有效。

GRANT ALL ON .全力以赴 TO 'root'@'%'; TO 'root'@'%';

Reference used - https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04#step-6-%E2%80%94-testing-database-connection-from-php-optional使用的参考 - https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04#step-6-%E2% 80%94-testing-database-connection-from-php-optional

Many thanks @ Nebulastic If you want to only allow remote IP using following command非常感谢@ Nebulastic如果您只想使用以下命令允许远程 IP

CREATE USER 'user_test'@'113.yy.xx.94' IDENTIFIED BY 'YOUR_PWD';
GRANT ALL ON *.* TO 'user_test'@'113.yy.xx.94';
FLUSH PRIVILEGES;

In my case I wanted to do something similar, I followed some steps from here but the best way was as @nebulasic mentioned:就我而言,我想做类似的事情,我从这里开始执行一些步骤,但最好的方法是 @nebulasic 提到的:

USE mysql;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'P@ssW0rd';
GRANT ALL ON *.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
  • ubuntu 22.04.1 ubuntu 22.04.1
  • Mysql Ver 8.0.31-0 Mysql 版本 8.0.31-0

My root had no GRANT privileges so I could not grant new users any previligies.我的 root 没有 GRANT 权限,所以我不能授予新用户任何特权。 Solution was to Drop current root user and create new one using 'mysql_native_password'.解决方案是删除当前 root 用户并使用“mysql_native_password”创建新用户。

Commands as follows Login to mysql with as root命令如下 以root身份登录mysql

mysql> DROP USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD' FROM mysql.user;
mysql> CREATE USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'locahost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

这可能有效:

grant all on dbtest.* to 'dbuser'@'%' identified by 'mysql_password';

I had this same issue, which led me here.我有同样的问题,这导致我来到这里。 In particular, for local development, I wanted to be able to do mysql -u root -p without sudo .特别是对于本地开发,我希望能够在没有sudo情况下执行mysql -u root -p I don't want to create a new user.我不想创建新用户。 I want to use root from a local PHP web app.我想从本地 PHP Web 应用程序使用root

The error message is misleading, as there was nothing wrong with the default 'root'@'%' user privileges.该错误消息具有误导性,因为默认的'root'@'%'用户权限没有任何问题

Instead, as several people mentioned in the other answers, the solution was simply to set bind-address=0.0.0.0 instead of bind-address=127.0.0.1 in my /etc/mysql/mysql.conf.d/mysqld.cnf config.相反,正如其他答案中提到的几个人,解决方案只是在我的/etc/mysql/mysql.conf.d/mysqld.cnf配置中设置bind-address=0.0.0.0而不是bind-address=127.0.0.1 . No changes were otherwise required.其他方面无需更改。

带有 sudo 的 Stary mysql

sudo mysql

我在 CentOS 上遇到了同样的问题,这对我有用(版本:8.0.11):

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'

This worked for me 这对我有用

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'password';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

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

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