mysql -v systemctl start mysqld systemctl enable mysqld systemctl status mysqld
查看随机生成的密码
grep "password" /var/log/mysqld.log
以root身份进入Mysql
mysql -uroot -p 输入刚刚获取到的密码即可进入
修改root的Mysql密码
密码要求大写字母+小写字母+数字+特殊字符,长度不能小于8位
alter user 'root'@'localhost' identified with mysql_native_password BY 'Lun5201314..';
可以将密码修改为简单模式,方便操作(非必要)
#设置为弱口令,密码最小长度为1 mysql> set global validate_password.policy=0; mysql> set global validate_password.length=1; mysql> flush privileges; #查看密码策略 mysql> show variables like '%validate_password_policy%'; mysql> show variables like '%validate_password_length%';
可以设置免密登录(非必要)
修改/etc/my.cnf文件
把skip-grant-tables添加到配置文件里面,放在[mysqld]下面就可以了
重启服务systemctl restart mysqld
可以对Mysql进行安全性配置(非必要)
mysql_secure_installation
重置root用户的密码。
说明:在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。 Enter password for user root: #输入已获取的root用户初始密码
The existing password for the user account root has expired. Please set a new password.
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码 The ‘validate_password’ component is installed on the server. The subsequent steps will run with the existing configuration of the component. Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。
New password: #输入新的MySQL密码
Re-enter new password: #重复输入新的MySQL密码
Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #输入Y确认使用已设置的密码。 b. 删除匿名用户。 c. By default, a MySQL installation has an anonymous user, d. allowing anyone to log into MySQL without having to have e. a user account created for them. This is intended only for f. testing, and to make the installation go a bit smoother. g. You should remove them before moving into a production h. environment. i. j. Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y #输入Y删除MySQL默认的匿名用户。 Success. k. 禁止root账号远程登录。 l. Normally, root should only be allowed to connect from m. ‘localhost’. This ensures that someone cannot guess at n. the root password from the network. o. p. Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y #输入Y禁止root远程登录。 Success. q. 删除test库以及对test库的访问权限。 r. By default, MySQL comes with a database named ‘test’ that s. anyone can access. This is also intended only for testing, t. and should be removed before moving into a production u. environment. v. w. x. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y #输入Y删除test库以及对test库的访问权限。 y. - Dropping test database… z. Success. aa. bb. - Removing privileges on test database… Success. cc. 重新加载授权表。 dd. Reloading the privilege tables will ensure that all changes ee. made so far will take effect immediately. ff. gg. Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y #输入Y重新加载授权表。 hh. Success. ii. All done!
开启远程连接
mysql> use mysql; mysql> update user set host ='%' where user ='root'
给root用户授权
mysql> grant all privileges on *.* to root@'%' with grant option; mysql> flush privileges;
创建远程登录MySQL的账号
#创建数据库用户2021232203322,并授予远程连接权限 mysql> create user '2021232203322'@'%' identified by 'Lun5201314..'; #为2021232203322用户授权数据库所有权限 mysql> grant all privileges on *.* to '2021232203322'@'%'; #刷新权限 mysql> flush privileges;