一、安装之前检查系统是否存在自带的mysql rpm源
[root@localhost ~]# rpm -qa |grep mysql # 查看是否存在mysql rpm源
[root@localhost ~]#
[root@localhost ~]# rpm -qa |grep mariadb # 查看是否存在mariadb rpm源
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]#
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]#
[root@localhost ~]# rpm -qa |grep mariadb
[root@localhost ~]#
二、安装过程
1、下载mysql的rpm源
https://repo.mysql.com # 在这个地址中找到自己想要安装的版本,复制为如下格式的连接
https://repo.mysql.com/mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]# curl -O https://repo.mysql.com/mysql80-community-release-el7-7.noarch.rpm
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11196 100 11196 0 0 9777 0 0:00:01 0:00:01 --:--:-- 9786
[root@localhost ~]# ls
anaconda-ks.cfg mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]#
2、安装mysql源
[root@localhost ~]# yum localinstall ./mysql80-community-release-el7-7.noarch.rpm # 安装上面下载好的rpm源
Loaded plugins: fastestmirror
Examining ./mysql80-community-release-el7-7.noarch.rpm: mysql80-community-release-el7-7.noarch
Marking ./mysql80-community-release-el7-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================
Package Arch Version Repository Size
================================================================================================================
Installing:
mysql80-community-release noarch el7-7 /mysql80-community-release-el7-7.noarch 10 k
Transaction Summary
=======================================================================================================================================
Install 1 Package
Total size: 10 k
Installed size: 10 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
Installing : mysql80-community-release-el7-7.noarch 1/1
Verifying : mysql80-community-release-el7-7.noarch 1/1
Installed:
mysql80-community-release.noarch 0:el7-7
Complete!
# 到此rpm源安装成功
[root@localhost ~]#
[root@localhost ~]# rpm -qa |grep mysql # 查看rpm源是否安装成功
mysql80-community-release-el7-7.noarch
[root@localhost ~]#
[root@localhost ~]# yum repolist enabled |grep mysql # 查看rpm源是否安装成功
mysql-connectors-community/x86_64 MySQL Connectors Community 206
mysql-tools-community/x86_64 MySQL Tools Community 94
mysql80-community/x86_64 MySQL 8.0 Community Server 367
[root@localhost ~]#
3、安装
[root@localhost ~]# yum install mysql-community-server # 安装mysql-community-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.31-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.31-1.el7 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-icu-data-files = 8.0.31-1.el7 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1()(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
---> Package mysql-community-client.x86_64 0:8.0.31-1.el7 will be installed
--> Processing Dependency: mysql-community-client-plugins = 8.0.31-1.el7 for package: mysql-community-client-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.31-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.31-1.el7 will be installed
---> Package mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-client-plugins.x86_64 0:8.0.31-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:8.0.31-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
mysql-community-server x86_64 8.0.31-1.el7 mysql80-community 64 M
Installing for dependencies:
libaio x86_64 0.3.109-13.el7 base 24 k
mysql-community-client x86_64 8.0.31-1.el7 mysql80-community 16 M
mysql-community-client-plugins x86_64 8.0.31-1.el7 mysql80-community 2.5 M
mysql-community-common x86_64 8.0.31-1.el7 mysql80-community 647 k
mysql-community-icu-data-files x86_64 8.0.31-1.el7 mysql80-community 2.1 M
mysql-community-libs x86_64 8.0.31-1.el7 mysql80-community 1.5 M
Transaction Summary
=======================================================================================================================================
Install 1 Package (+6 Dependent packages)
Total download size: 87 M
Installed size: 409 M
Is this ok [y/d/N]: y
Downloading packages:
(1/7): libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:01
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Public key for mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm is not installed
(2/7): mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm | 2.5 MB 00:00:04
(3/7): mysql-community-common-8.0.31-1.el7.x86_64.rpm | 647 kB 00:00:00
(4/7): mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm | 2.1 MB 00:00:01
(5/7): mysql-community-libs-8.0.31-1.el7.x86_64.rpm | 1.5 MB 00:00:03
(6/7): mysql-community-client-8.0.31-1.el7.x86_64.rpm | 16 MB 00:00:19
(7/7): mysql-community-server-8.0.31-1.el7.x86_64.rpm | 64 MB 00:00:40
---------------------------------------------------------------------------------------------------------------------------------------
Total 1.7 MB/s | 87 MB 00:00:50
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Importing GPG key 0x3A79BD29:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: 859b e8d7 c586 f538 430b 19c2 467b 942d 3a79 bd29
Package : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Is this ok [y/N]: y
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-client-plugins-8.0.31-1.el7.x86_64 1/7
Installing : mysql-community-common-8.0.31-1.el7.x86_64 2/7
Installing : mysql-community-libs-8.0.31-1.el7.x86_64 3/7
Installing : mysql-community-client-8.0.31-1.el7.x86_64 4/7
Installing : libaio-0.3.109-13.el7.x86_64 5/7
Installing : mysql-community-icu-data-files-8.0.31-1.el7.x86_64 6/7
Installing : mysql-community-server-8.0.31-1.el7.x86_64 7/7
Verifying : mysql-community-server-8.0.31-1.el7.x86_64 1/7
Verifying : mysql-community-common-8.0.31-1.el7.x86_64 2/7
Verifying : mysql-community-client-plugins-8.0.31-1.el7.x86_64 3/7
Verifying : mysql-community-icu-data-files-8.0.31-1.el7.x86_64 4/7
Verifying : mysql-community-client-8.0.31-1.el7.x86_64 5/7
Verifying : libaio-0.3.109-13.el7.x86_64 6/7
Verifying : mysql-community-libs-8.0.31-1.el7.x86_64 7/7
Installed:
mysql-community-server.x86_64 0:8.0.31-1.el7
Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7 mysql-community-client.x86_64 0:8.0.31-1.el7
mysql-community-client-plugins.x86_64 0:8.0.31-1.el7 mysql-community-common.x86_64 0:8.0.31-1.el7
mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7 mysql-community-libs.x86_64 0:8.0.31-1.el7
Complete!
[root@localhost ~]#
# 到此安装成功
4、查看是否安装成功
[root@localhost ~]# yum list installed mysql* # 查看已安装的mysql相关的软件包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Installed Packages
mysql-community-client.x86_64 8.0.31-1.el7 @mysql80-community
mysql-community-client-plugins.x86_64 8.0.31-1.el7 @mysql80-community
mysql-community-common.x86_64 8.0.31-1.el7 @mysql80-community
mysql-community-icu-data-files.x86_64 8.0.31-1.el7 @mysql80-community
mysql-community-libs.x86_64 8.0.31-1.el7 @mysql80-community
mysql-community-server.x86_64 8.0.31-1.el7 @mysql80-community
mysql80-community-release.noarch el7-7 @/mysql80-community-release-el7-7.noarch
[root@localhost ~]#
[root@localhost ~]# systemctl status mysqld # 查看mysql服务状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@localhost ~]#
[root@localhost ~]# systemctl start mysqld # 启动mysql服务
[root@localhost ~]# systemctl status mysqld # 查看mysql服务状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-12-28 13:46:21 CST; 2s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 64032 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 64110 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─64110 /usr/sbin/mysqld
Dec 28 13:46:19 localhost.localdomain systemd[1]: Starting MySQL Server...
Dec 28 13:46:21 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#
到此mysql安装成功。接下来配置mysql服务。
三、mysql配置
1、查看mysql安装生成的root用户密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2022-12-28T05:46:19.928855Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: do,-moX%)2do
[root@localhost ~]#
# 生成的默认密码为:do,-moX%)2do
2、使用默认密码登录mysql
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.31
Copyright (c) 2000, 2022, 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>
3、更改root用户默认改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; # 如将root用户本地登陆密码改为123456
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements # 该报错原因是新密码不满足密码策略
mysql>
mysql> SHOW VARIABLES LIKE "%password%"; # 查看现有密码策略
+----------------------------------------------+-----------------+
| Variable_name | Value |
+----------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON |
| caching_sha2_password_digest_rounds | 5000 |
| caching_sha2_password_private_key_path | private_key.pem |
| caching_sha2_password_public_key_path | public_key.pem |
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| generated_random_password_length | 20 |
| mysql_native_password_proxy_users | OFF |
| password_history | 0 |
| password_require_current | OFF |
| password_reuse_interval | 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 | ON |
| 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 |
+----------------------------------------------+-----------------+
23 rows in set (0.01 sec)
# validate_password.length 密码长度
# validate_password.policy 含有字母大小的啥的
mysql>
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE "%password%"; # 查看更改后的密码策略
+----------------------------------------------+-----------------+
| Variable_name | Value |
+----------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON |
| caching_sha2_password_digest_rounds | 5000 |
| caching_sha2_password_private_key_path | private_key.pem |
| caching_sha2_password_public_key_path | public_key.pem |
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| generated_random_password_length | 20 |
| mysql_native_password_proxy_users | OFF |
| password_history | 0 |
| password_require_current | OFF |
| password_reuse_interval | 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 | ON |
| validate_password.dictionary_file | |
| validate_password.length | 4 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+----------------------------------------------+-----------------+
23 rows in set (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; # 如将root用户本地登陆密码改为123456
Query OK, 0 rows affected (0.00 sec)
mysql>
# 改密成功
4、设置远程访问数据库用户和密码
两种方式
方式一(我失败了,但参数说明可供参考)
grant all privileges on *.* to 'root' @'%' identified by '123456';
第一个*是数据库,可以改成允许访问的数据库名称
第二个 是数据库的表名称,代表允许访问任意的表
root代表远程登录使用的用户名,可以自定义
%代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了
password代表远程登录时使用的密码,可以自定义
让权限立即生效:flush privileges
mysql> grant all privileges on *.* to 'root' @'%' identified by '123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456'' at line 1
方式二(成功)
# 创建用户
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
# 授权
grant all privileges on *.* to 'root'@'%' ;
# 刷新访问权限表
FLUSH PRIVILEGES;
四、远程连接mysql
这里使用的远程连接mysql的工具为DataGrip
异常原因为防火墙为开放3306端口,这里直接将防火墙进行关闭
# 关闭防火墙
systemctl stop firewalld.service
# 查看防火墙状态
firewall-cmd --state 或者 systemctl status firewalld.service
以下为解决上述异常碰到的小插曲以为是selinux引起的,引以为戒
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1