1.远程登录Amazon Linux
用ssh-keygen生成ssh密钥
-t:生成 RSA 类型的密钥-f:指定密钥存储路径
ssh-keygen -t rsa -f ~/.ssh/ec2
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/chessia/.ssh/ec2
Your public key has been saved in /Users/chessia/.ssh/ec2.pub
The key fingerprint is:
SHA256:7Ks0TN0GVrL1WAHzZh496aFkrlQrW8wkFNBB+0adNbo chessia@CdeMacBook-Pro.local
The key's randomart image is:
+---[RSA 3072]----+
| o+X+o. ..|
| *.B o.+.|
| + + %.B |
| + o / =.o |
| . S = @E. |
| o . o * |
| + . o |
| . . . |
| ... |
+----[SHA256]-----+
在MacOS/Linux下,把生成的ec2.pub上传到Amazon服务器上(EC2)
cat ec2.pub|pbcopy
用ssh远程登录
-i:指定私钥路径
ssh -i ~/.ssh/ec2 ec2-user@18.183.29.171
The authenticity of host '18.183.29.171 (18.183.29.171)' can't be established.
ED25519 key fingerprint is SHA256:Ti5nq4V7tCdBD6PBMAtLWlVV/ZO5LW/R8Mx4ry6kPLM.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '18.183.29.171' (ED25519) to the list of known hosts.
Enter passphrase for key '/Users/chessia/.ssh/ec2':
, #_
~\_ ####_ Amazon Linux 2
~~ \_#####\
~~ \###| AL2 End of Life is 2025-06-30.
~~ \#/ ___
~~ V~' '->
~~~ / A newer version of Amazon Linux is available!
~~._. _/
_/ _/ Amazon Linux 2023, GA and supported until 2028-03-15.
_/m/' https://aws.amazon.com/linux/amazon-linux-2023/
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
进入root
sudo -i
查看系统信息
cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
SUPPORT_END="2025-06-30"
允许root用户登录
sudo vim /etc/ssh/sshd_config
将PermitRootLogin no修改为PermitRootLogin yes,保存文件后重启ssh服务
sudo systemctl restart sshd
2.下载Mysql
首先删除已有的 mariadb-server(如果已安装)
sudo yum remove mariadb-* -y
下载并安装 MySQL yum 仓库
-i:安装指定的.rpm包-v:输出详细信息-h:显示进度条
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
sudo rpm -ivh mysql80-community-release-el7-7.noarch.rpm
安装 MySQL 8.0
sudo yum install mysql-community-server -y
如果遇到 GPG 密钥错误
Transaction Summary
================================================================================
Install 1 Package (+6 Dependent packages)
Total download size: 89 M
Installed size: 417 M
Downloading packages:
warning: /var/cache/yum/x86_64/2/mysql80-community/packages/mysql-community-client-plugins-8.0.40-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID a8d3785c: NOKEY
Public key for mysql-community-client-plugins-8.0.40-1.el7.x86_64.rpm is not installed
(1/7): mysql-community-client-plugins-8.0.40-1.el7.x86_64. | 3.5 MB 00:00
(2/7): mysql-community-common-8.0.40-1.el7.x86_64.rpm | 665 kB 00:00
(3/7): mysql-community-icu-data-files-8.0.40-1.el7.x86_64. | 2.2 MB 00:00
(4/7): mysql-community-libs-8.0.40-1.el7.x86_64.rpm | 1.5 MB 00:00
(5/7): mysql-community-client-8.0.40-1.el7.x86_64.rpm | 16 MB 00:00
(6/7): ncurses-compat-libs-6.0-8.20170212.amzn2.1.8.x86_64 | 309 kB 00:00
(7/7): mysql-community-server-8.0.40-1.el7.x86_64.rpm | 65 MB 00:00
--------------------------------------------------------------------------------
Total 145 MB/s | 89 MB 00:00
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 (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
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 (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-client-plugins-8.0.40-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-client-plugins-8.0.40-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022, file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
重新导入 MySQL 的 GPG 密钥
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql
清理 yum 缓存并重新安装
sudo yum clean all
sudo yum makecache
重新安装 MySQL
sudo yum install mysql-community-server --nogpgcheck -y
Installed:
mysql-community-server.x86_64 0:8.0.40-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:8.0.40-1.el7
mysql-community-client-plugins.x86_64 0:8.0.40-1.el7
mysql-community-common.x86_64 0:8.0.40-1.el7
mysql-community-icu-data-files.x86_64 0:8.0.40-1.el7
mysql-community-libs.x86_64 0:8.0.40-1.el7
ncurses-compat-libs.x86_64 0:6.0-8.20170212.amzn2.1.8
Complete!
使用 --nogpgcheck 参数可以暂时跳过 GPG 检查。虽然通常不建议跳过安全检查,但在这种情况下,由于是直接从 MySQL 官方仓库下载,相对安全
安装完成后,启动 MySQL 服务
sudo systemctl start mysqld
如果提示 Unit not found,需要重新加载系统服务
sudo systemctl daemon-reload
sudo systemctl start mysqld
检查 MySQL 服务状态
sudo systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2025-01-20 16:06:38 UTC; 39s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 2880 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 2948 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─2948 /usr/sbin/mysqld
设置开机自启
sudo systemctl enable mysqld
获取临时密码
sudo grep 'temporary password' /var/log/mysqld.log
A temporary password is generated for root@localhost: RefaOhs7Bn)l
运行安全配置脚本
sudo mysql_secure_installation
在运行安全配置脚本时,需要:
- 输入之前获取的临时密码
- 设置新的 root 密码(必须包含大小写字母、数字和特殊字符)
- 删除匿名用户
- 禁止 root 远程登录
- 删除测试数据库
- 重新加载权限表 验证安装
mysql -V
mysql Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)
登录
mysql -uroot -p
如果登陆不成功,出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2),则需要修改/etc/my.cnf,添加如下代码
sudo vim /etc/my.cnf
[mysqld]
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
# 重启
sudo systemctl restart mysqld