1.修改配置文件,获取权限登录[email protected]#vi /etc/my,cnf添加:skip-grant-tables[email protected]#systemctl restart MysqLd2.登录创建root用户[email protected]#MysqLMysqL>use MysqL; insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';3.更新root用户权限update user set Host=‘localhost’,select_priv=‘y’,insert_priv=‘y’,update_priv=‘y’,Alter_priv=‘y’,delete_priv=‘y’,create_priv=‘y’,drop_priv=‘y’,reload_priv=‘y’,shutdown_priv=‘y’,Process_priv=‘y’,file_priv=‘y’,grant_priv=‘y’,References_priv=‘y’,index_priv=‘y’,create_user_priv=‘y’,show_db_priv=‘y’,super_priv=‘y’,create_tmp_table_priv=‘y’,Lock_tables_priv=‘y’,execute_priv=‘y’,repl_slave_priv=‘y’,repl_clIEnt_priv=‘y’,create_vIEw_priv=‘y’,show_vIEw_priv=‘y’,create_routine_priv=‘y’,alter_routine_priv=‘y’,create_user_priv=‘y’ where user=‘root’;4.修改配置文件权限将1中的–skip-grant-tables删除[email protected]#systemctl restart MysqLd[email protected]#MysqL -uroot -ppassword: -----密码为空5.修改密码MysqL>use MysqL;使用命令更改root密码:UPDATE user SET Password=PASSWORD('root') where USER='root';刷新权限:FLUSH PRIVILEGES;然后退出,重新登录: quit方法二:Python 脚本
import pyMysqLimport os#修改配置文件获取权限print("modify my.cnf skip grant tables.........")os.system("echo'-skip-grant-tables'>>/etc/my.cnf")k = os.system("systemctl restart MysqLd")if k==0: pint("erro:",k)else: print("modify success!") #连接数据库 db=pyMysqL.connect("127.0.0.1") cursor=db.cursor() cursor.execute("use MysqL") #添加root用户并更新root权限 sql_SYN="insert into user(User,authentication_string,ssl_cipher x509_issuer x509_subject) values('root','','')" cursor.execute(sql_SYN) sql_SYN="update user set Host='%',select_priv='y',insert_priv='y',update_priv='y',Alter_priv='y',delete_priv='y',create_priv='y',drop_priv='y',reload_priv='y',shutdown_priv='y',Process_priv='y',file_priv='y',grant_priv='y',References_priv='y',index_priv='y',create_user_priv='y',show_db_priv='y',super_priv='y',create_tmp_table_priv='y',Lock_tables_priv='y',execute_priv='y',repl_slave_priv='y',repl_clIEnt_priv='y',create_vIEw_priv='y',show_vIEw_priv='y',create_routine_priv='y',alter_routine_priv='y',create_user_priv='y' where user='root'" cursor.execute(sql_SYN) db.close() #修改配置文件取消权限 os.system("sed -i '/-skip-grant/d' /etc/my.cnf") print("modify my.cnf skip grant tables.........") k=os.system("systemctl restart MysqLd") if k!=0: print("erro:",k) else: print("modify success!") db = pyMysqL.connect("127.0.0.1","root","","MysqL") #修改密码 sql_SYN="alter user 'root'@'%' IDentifIEd with MysqL_native_password by '[email protected]'" db.cursor(sql_SYN) #权限设置 sql_SYN="grant all privileges on *.* to 'root'@'%'" db.cursor(sql_SYN) db.close() print("root user adding success!! The password of 'root' is '[email protected]'")总结
以上是内存溢出为你收集整理的误删root用户,如何恢复全部内容,希望文章能够帮你解决误删root用户,如何恢复所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)