linux MySQL账号密码管理|不记得密码也可
1、linux mysql5.6.26 忘记密码怎么办
在Linux中忘记MySQL密码后,可以通过以下步骤来进行重置密码:
停止MySQL服务:在终端输入 sudo service mysql stop 命令
# sudo service mysql stop
Shutting down MySQL. SUCCESS!
以跳过授权表的方式启动MySQL:在终端输入 sudo mysqld_safe --skip-grant-tables & 命令
# 小白版哈哈
# sudo mysqld_safe --skip-grant-tables &
[1] 20839
# sudo: mysqld_safe: command not found
# 实际版win
# locate mysqld_safe
/usr/local/mysql-5.6.26/bin/mysqld_safe
/usr/local/mysql-5.6.26/man/mysqld_safe.1
/usr/local/mysql-5.6.26/man/man1/mysqld_safe.1
/usr/local/mysql-5.6.26/scripts/mysqld_safe
/usr/local/mysql-5.6.26/scripts/mysqld_safe.sh
# sudo /usr/local/mysql-5.6.26/bin/mysqld_safe --skip-grant-tables &
[1] 22061
# 230512 12:13:06 mysqld_safe Logging to '/usr/local/mysql/data/xxip.v-sz-1.company.lan.err'.
230512 12:13:06 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
连接MySQL:在终端输入 mysql -u root 命令
# 230512 12:13:06 mysqld_safe Logging to '/usr/local/mysql/data/xxip.v-sz-1.company.lan.err'.
230512 12:13:06 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.26-log Source distribution
Copyright (c) 2000, 2015, 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>
切换到mysql数据库:在MySQL命令行中输入 use mysql; 命令
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
重置root用户密码:在MySQL命令行中输入 UPDATE user SET password=PASSWORD(‘your_new_password’) WHERE User=‘root’; 命令。需要将 your_new_password 替换成你想要设置的密码。
mysql> UPDATE user SET password=PASSWORD('XXXXXXXhhhhh') WHERE User='root';
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql>
刷新授权表:在MySQL命令行中输入 FLUSH PRIVILEGES; 命令
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
退出MySQL:在MySQL命令行中输入 exit 命令
mysql> exit
Bye
重启MySQL服务:在终端输入 sudo service mysql start 命令
# sudo service mysql start
230512 12:15:33 mysqld_safe mysqld from pid file /usr/local/mysql/data/xxip.v-sz-1.company.lan.pid ended
Starting MySQL. SUCCESS!
[1]+ Done sudo /usr/local/mysql-5.6.26/bin/mysqld_safe --skip-grant-tables
完成以上步骤后,你的MySQL密码将被成功重置。
2、linux mysql5.6.26 修改指定账号的密码
在Linux中,如果要修改MySQL5.6.26中的指定账号的密码,可以按照以下步骤进行操作:
以root身份登录MySQL:在终端输入 mysql -u root -p 命令,然后输入root用户的密码进行登录。
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.26-log Source distribution
Copyright (c) 2000, 2015, 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>
切换到要修改密码的账号所在的数据库:在MySQL命令行中输入 use mysql; 命令。
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
查看当前用户列表:在MySQL命令行中输入 select User, Host from user; 命令,可以查看当前所有用户列表,找到要修改密码的账号的用户名和主机信息。
+------+-----------------------------------------------------+
| User | Host |
+------+-----------------------------------------------------+
| root | % |
| tars | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| tars | localhost |
| | hhhhhh |
| root | hhhhh |
+------+-----------------------------------------------------+
9 rows in set (0.00 sec)
mysql>
修改密码:在MySQL命令行中输入以下命令:
update user set password=PASSWORD(‘new_password’) where User=‘username’ and Host=‘hostname’;
其中,new_password为新密码,username为要修改密码的账号的用户名,hostname为要修改密码的账号的主机名。输入完命令后按回车键执行。
mysql> update user set password=PASSWORD('xxxxx') where User='tars' and Host='%';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update user set password=PASSWORD('xxxxx') where User='tars' and Host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
刷新权限表,让修改后的密码生效:在MySQL命令行中输入 flush privileges; 命令。
退出MySQL:在MySQL命令行中输入 exit 命令。
完成以上操作后,指定账号的密码就已经被修改了。可以使用新密码登录该账号。
3、linux mysql5.6.26 删除指定账号
在Linux中,如果要删除MySQL5.6.26中的指定账号,可以按照以下步骤进行操作:
以root身份登录MySQL:在终端输入 mysql -u root -p 命令,然后输入root用户的密码进行登录。
切换到用户所在的数据库:在MySQL命令行中输入 use mysql; 命令。
查看当前用户列表:在MySQL命令行中输入 select User, Host from user; 命令,可以查看当前所有用户列表,找到要删除的账号的用户名和主机信息。
删除用户:在MySQL命令行中输入以下命令:
delete from user where User=‘username’ and Host=‘hostname’;
其中,username为要删除账号的用户名,hostname为要删除账号的主机名。输入完命令后按回车键执行。
mysql> delete from user where User='' and Host='localhost';
Query OK, 1 row affected (0.00 sec)
mysql> delete from user where User='' and Host='hhhhhh';
Query OK, 1 row affected (0.00 sec)
刷新权限表,让修改生效:在MySQL命令行中输入 flush privileges; 命令。
退出MySQL:在MySQL命令行中输入 exit 命令。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
完成以上操作后,指定账号就已经被删除了。注意,删除账号并不会删除该账号创建的库和表,需要另外进行处理。
4、 查看是否启动mysql
systemctl status mysql
systemctl start mysql 启动服务
5、切记:最后要终止安全模式进程?
关闭安全模式shell会话窗口,kill掉mysqld_safe mysqld两个进程,如果不做这个步骤会有安全隐患。
查看MySQL进程列表
$ ps -A | grep mysql
2491 ? 00:00:00 mysqld_safe
2751 ? 00:00:00 mysqld
终止进程
$ kill -9 2491 2751
PS:不知道mysqld是啥,我只kill了mysqld_safe,。。
mysql和mysqld的区别
1.mysqld是后台服务程序,即mysql服务器,是一个服务,linux中的服务通常以d结尾,缩写是daemon,守护的意思
2.mysql是一个交互式输入sql语句或从sql文件批处理它们的一个命令行工具,它相当于一个客户端软件,可以对服务端mysqld发起连接。
https://blog.youkuaiyun.com/gaoshanliushui131/article/details/104540209