1、下载并安装MySQL官方的 Yum Repository
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2、检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
[root@localhost ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 131
mysql-tools-community/x86_64 MySQL Tools Community 100
mysql57-community/x86_64 MySQL 5.7 Community Server 384
安装成功
3、修改yum源 ,选择安装的mysql版本(可跳过,默认安装的版本是5.7)
$ vim /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。
4、安装MySQL
yum install mysql-community-server
5、启动MySQL服务并设置开机启动
systemctl start mysqld systemctl enable mysqld systemctl daemon-reload
6、查看MySQL运行状态,运行状态如下:
systemctl status mysqld.service
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-12-23 09:50:21 CST; 2s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 26205 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 26149 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 26209 (mysqld)
Tasks: 27
CGroup: /system.slice/mysqld.service
└─26209 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
7、放开防火墙端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload
8、修改Mysql默认密码
1、查找安装时生成的临时密码
grep "password" /var/log/mysqld.log
2019-12-23T01:50:18.061785Z 1 [Note] A temporary password is generated for root@localhost: uaXGt(m&y4jI
2、连接mysql
mysql -uroot -p
3、修改密码【注意:后面的分号一定要跟上】
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
或者:
set password for 'root'@'localhost'=password('MyNewPass4!');
注意密码的长度和复杂度,如果长度和复杂度不够会报错的。
4、查看密码策略
mysql> show variables like '%password%'; +----------------------------------------+-----------------+ | Variable_name | Value | +----------------------------------------+-----------------+ | default_password_lifetime | 0 | | disconnect_on_expired_password | ON | | log_builtin_as_identified_by_password | OFF | | mysql_native_password_proxy_users | OFF | | old_passwords | 0 | | report_password | | | sha256_password_auto_generate_rsa_keys | ON | | sha256_password_private_key_path | private_key.pem | | sha256_password_proxy_users | OFF | | sha256_password_public_key_path | public_key.pem | | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +----------------------------------------+-----------------+ 17 rows in set (0.01 sec)
9、卸载Yum Repository
至此,mysql数据库已经安装完成了,但还有一个问题,就是因为安装了Yum Repository,以后每次yum update 操作都会自动更新,需要把这个卸载掉:
yum -y remove mysql57-community-release-el7-10.noarch
执行以下命令检查下:
yum repolist enabled | grep "mysql.*-community.*"