简体   繁体   English

在mysql中重新创建一个“ mysql.user”表

[英]Recreate a `mysql.user` table in mysql

我发现我的mysql.user表被弄乱了, drop table if exists mysql.user来删除它,请使用drop table if exists mysql.user ,如何手动创建此特殊表并向其中添加新帐户。

NB I have never tried this so I have no idea if it will work at all. 注意:我从未尝试过这样做,所以我完全不知道它是否会起作用。 YOU HAVE BEEN WARNED!! 你被警告了!!

Before you do anything, shut down the MySQL service and take a backup of the data folder (copy it elsewhere). 在执行任何操作之前,请关闭MySQL服务并备份数据文件夹(将其复制到其他位置)。

Next, either try creating a new data directory and run mysql_install_db (from the MySQL install directory, scripts/mysql_install_db --datadir=<new data directory> ), or use a virtual machine and do the same. 接下来,尝试创建一个新的数据目录并运行mysql_install_db (从MySQL安装目录, scripts/mysql_install_db --datadir=<new data directory> ),或使用虚拟机并执行相同的操作。 Whatever you do, do NOT install into the original folder. 无论做什么,都不要安装到原始文件夹中。 Use the same version of MySQL to do this 使用相同版本的MySQL来执行此操作

Whichever method you use, take the resultant files from <mysql data folder>/data (they should be called user.MYD , user MYI and user.frm ) and copy them into your broken folder. 无论使用哪种方法,都从<mysql data folder>/data获取结果文件(它们应称为user.MYDuser MYIuser.frm )并将它们复制到损坏的文件夹中。 Ensure that user rights are the same as the rest of the folder (owner and group should be read/write). 确保用户权限与文件夹的其余部分相同(所有者和组应为读/写)。

Restart your MySQL service and see if you can get into the system (no login information will be required, but you will have to log in to MySQL as root . If you can, recreate the relevant users for application access. If not, you still have a backup of the data folder. 重新启动MySQL服务,并查看是否可以进入系统(不需要登录信息,但是您必须以root身份登录MySQL。如果可以,请重新创建相关的用户以进行应用程序访问。备份数据文件夹。

In the event that it doesn't work, and assuming that you have recent backups, re-create the data folder completely using mysql_install_db (ensuring that you still have the copy of the folder you took before you started out), create the users, then restore your individual databases one by one from your last good backup. 万一它不起作用,并且假设您最近备份过,请使用mysql_install_db完全重新创建数据文件夹(确保您仍然拥有开始之前创建的文件夹的副本),然后创建用户,然后从上次良好的备份中逐个还原单个数据库。 Do not restore the mysql database and tables - they will be built as you restore the other databases. 不要还原mysql数据库和表-它们将在还原其他数据库时构建。

Try all this out on a virtual machine first - it'll help you find any problems along the way 首先在虚拟机上尝试所有步骤-它将帮助您一路上发现任何问题

Good luck 祝好运

Thanks to @DaveyBoy 's answer . 感谢@DaveyBoy的回答 I operated like below on ubuntu 16.04, mysql 5.7.20 , and got mysql.user table back: 我在ubuntu 16.04,mysql 5.7.20上进行如下操作,并获得了mysql.user表:

sudo mysqld --initialize --datadir=~/data_tmp

sudo cp ~/data_tmp/mysql/user.frm /var/lib/mysql/mysql
sudo cp ~/data_tmp/mysql/user.MYD /var/lib/mysql/mysql
sudo cp ~/data_tmp/mysql/user.MYI /var/lib/mysql/mysql

sudo chown mysql:mysql /var/lib/mysql/mysql/user.frm
sudo chown mysql:mysql /var/lib/mysql/mysql/user.MYD
sudo chown mysql:mysql /var/lib/mysql/mysql/user.MYI

In mysql cli select * from mysql.user , table mysql.user should show up now. mysql cli中, select * from mysql.user ,表mysql.user应该会显示出来。

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

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