1.1 命令行终端输入:
mysqladmin -uroot -p password 密码
回车,提示输入密码,但是mysql初始化时没有密码,所以直接又是回车
1.2 验证mysql密码是否设置成功:
mysql -uroot -p
回车,输入密码,进入mysql页面
2.1 在mysql命令行输入以下sql命令(alter开头的命令):
mysql> alter user root@主机 identified by 密码;
2.2 exit退出mysql后,再使用mysql -hlocalhost -uroot -p密码,回车进入数据库
安装mysql-server后:
[root@localhost ~]# systemctl enable mysqld --now
[root@localhost ~]# mysql
回车后进入mysql命令行:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> alter user root@"localhost" identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> exit
推出mysql后使用:
mysql -hlocalhost -uroot -p123456
重新进入mysql,成功进入mysql就说明设置密码成功
mysqladmin -uroot -p password 新密码
回车,输入旧密码
验证mysql密码是否修改成功:
mysql -uroot -p
回车,输入新密码
mysql> alter user root@"localhost" identified by "1234";
1)vim /etc/my.cnf.d/mysql-server.cnf
加一行:
skip-grant-tables
其他不变
2)systemctl restart mysqld
此时不会验证密码,方便无密码进入mysql,清空authentication_string
[root@localhost ~]# mysqladmin -uroot -p password "1234"
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@localhost ~]# mysql -uroot -p
Enter password:
#清空authentication_string
mysql> update mysql.user set authentication_string='' where host='localhost' and user='root';
#查看root的authentication_string的值是否清空了
mysql> select user,authentication_string from mysql.user;
#重新加载授权表,使新的授权规则立即生效
mysql> flush privileges;
exit推出,将mysqld的配置改回去并重启mysqld:
1)vim /etc/my.cnf.d/mysql-server.cnf
注释掉 skip-grant-tables
其他不变
2)systemctl restart mysqld
再使用“mysql”命令就可以直接进入数据库;
exit退出数据库,再走一遍设置密码的流程就行了(请查看“一、数据库设置密码”)。