操作系统:CentOS7.9
配置:4C/8G
数据库版本:MySQL5.7.44
本机IP:192.168.17.2
说明:如果已经安装完成MySQL直接从第二步设置权限开始,第一不非必须。
一.修改初始密码
方式一
日常使用的密码修改方式,推荐使用。
#先修改密码
[root@localhost ~]# grep password /var/log/mysqld.log
2024-03-10T08:31:50.987694Z 1 [Note] A temporary password is generated for root@localhost: sUVwmT_q#1NG
[root@localhost ~]# mysql -uroot -p'sUVwmT_q#1NG'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44
Copyright (c) 2000, 2023, 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.
#密码应该为:小写字母,大写字母,符号,数字,字符数>8
mysql> ALTER USER 'root'@'localhost'IDENTIFIED BY 'Linux2024312.';
Query OK, 0 rows affected (0.00 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
方式二
设置弱口令,设置简单密码,不能用于生产环境。
[root@localhost ~]# grep password /var/log/mysql.err
2024-03-10T08:49:07.809029Z 1 [Note] A temporary password is generated for root@localhost: !stY5Ofyya4P
[root@localhost ~]# mysql -uroot -p'!stY5Ofyya4P'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log
Copyright (c) 2000, 2023, 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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#设置弱口令
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
#可以修改简单的密码
mysql> alter user 'root'@'localhost' identified by '123123';
Query OK, 0 rows affected (5.00 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
二.设置权限
[root@localhost ~]# mysql -uroot -p'123123'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44-log MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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表
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
#用 % 能让任何 host 连接,也可以设置特定的 IP
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
三.Navicat连接数据库
主机IP写本机IP,端口没修改过默认3306,用户名root,密码是自己设置的
可以点测试连接,看看能否成功
让后点确认
到这里就结束了!