MySQL安装及初始密码设置方式
作者:炽天使YRLT
运行mysql --help | grep my.cnf查看my.cnf配置位置的读取顺序。
/etc/my.cnf不存在,则我们需要在etc下创建my.cnf配置文件(mysql会优先度读取)。
ps -ef|grep mysqld
mysql不要随意修改配置文件,因为修改了它可能就启动不了了
- 下载MySQL包
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
- 安装MySQL源
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
- 安装MySQL
yum install -y mysql-community-server
使用yum -y install mysql-community-server安装mysql时候可能提示以下错误
The GPG keys listed for the “MySQL 5.7 Community Server” repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.Failing package is: mysql-community-libs-compat-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
原因是Mysql的GPG升级了,需要重新获取
- 使用以下命令即可
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后要启动MySQL,要设置初始密码
1.例如你的 root用户现在没有密码,你希望的密码修改为123456,那么命令是:
mysqladmin -u root password 123456
但是这又有一个问题,它显示找不到mysqladmin这个命令
解决办法如下:
首先找到MySQL的安装路径,找到mysql,mysqladmin的位置
- 使用命令
whereis mysql whereis mysqladmin
- 创建软链接
ln -s /usr/local/mysql/bin/mysql /usr/bin ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
- 启动MySQL
systemctl start mysqld.service
- 查找初始密码
[root@VM-4-13-centos bin]# grep "password" /var/log/mysqld.log 2022-11-11T13:30:29.233762Z 1 [Note] A temporary password is generated for root@localhost: dnqPGeySk6_8 2022-11-11T13:59:07.201871Z 2 [Note] Access denied for user 'root'@'localhost' (using password: YES) 2022-11-11T13:59:16.191045Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2022-11-11T13:59:46.683483Z 4 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2022-11-11T14:00:06.485081Z 5 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2022-11-11T14:00:11.311448Z 6 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2022-11-11T14:01:12.576128Z 7 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2022-11-11T14:01:54.863154Z 8 [Note] Access denied for user 'root'@'localhost' (using password: NO)
1.例如你的 root用户现在没有密码,你希望的密码修改为123456,那么命令是:
mysqladmin -u root password 123456
2.如果你的root现在有密码了(123456),那么修改密码为abcdef的命令是:
mysqladmin -u root -p password abcdef
注意,命令回车后会问你旧密码,输入旧密码123456之后命令完成,密码修改成功。
3.如果你的root现在有密码了(123456),那么修改密码为abcdef的命令是:
mysqladmin -u root -p123456 password abcdef (注意-p 不要和后面的密码分开写,要写在一起,不然会出错,错误如下所示)
更好的方式是使用命令查询初始密码
然后登陆进入MySQL
使用命令行修改密码,但是如果我们设置的密码复杂度不够,它是不给设置的,所以要修改一下配置
set global validate_password_policy=0; # 保密级别 set global validate_password_length=1; # 密码长度
然后修改密码
ALTER USER USER() IDENTIFIED BY '您的新密码;
至此就OK了~
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。