CentOS 7 安装MySQL5.7

194 阅读3分钟

官方下载地址

downloads.mysql.com/archives/co…

官网安装文档

dev.mysql.com/doc/refman/…

官方安装步骤

下载的软件放在/usr/local/itxuhang目录下,但是建立的软链接mysql是放在/usr/local目录下

1.下载压缩包

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

2.解压

tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

3.准备一些依赖包

由于MySQL初始化数据时依赖了libaio,如果你的服务器没有的话可能会出错,为了保险起见,建议安装一下

根据不同系统选择不同命令执行

  • Centos系统
yum search all libaio
yum install libaio
  • Ubuntu系统
apt-cache search libaio # search for info
apt-get install libaio1 # install library

4.安装并初始化

  • 创建用户组和用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
  • 建立软链接(类似于Windows的快捷方式)
cd /usr/local
ln -s /usr/local/itxuhang/mysql-5.7.35-linux-glibc2.12-x86_64 mysql
  • 创建mysql-files目录,并授权
cd mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
  • 初始化
bin/mysqld --initialize --user=mysql
  • 启动MySQL
bin/mysql_ssl_rsa_setup
bin/mysqld_safe --user=mysql &
  • 可选项
# Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server

注意:

新数据库的临时密码在最后一行

[root@VM-12-7-centos mysql]# bin/mysqld --initialize --user=mysql
2022-05-10T11:06:29.792079Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-05-10T11:06:30.531624Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-05-10T11:06:30.603606Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-05-10T11:06:30.673555Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3ed5457b-d051-11ec-9545-525400dd4edb.
2022-05-10T11:06:30.683388Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-05-10T11:06:31.341770Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T11:06:31.341786Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T11:06:31.342355Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-10T11:06:31.483054Z 1 [Note] A temporary password is generated for root@localhost: WdGh,l.?6!h3

5.登录

命令:

mysql -u root -p

提示:如果报如下错误:

可执行如下命令

ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

如果忘记临时密码,可以在/etc/my.cnf文件中配置skip-grant-tables,这样在登录时就不需要填写密码了

skip-grant-tables

登录成功会出现下图所示:

6.修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'itxuhang';

创建root账号允许远程登录,并授予所有权限。

CREATE USER 'root'@'%' IDENTIFIED BY 'itxuhang';
GRANT ALL ON *.* TO 'root'@'%';

7.退出

exit;
\quit
\q

8.用新密码登录

9.停止服务

mysql.server stop

通过向服务器发送信号来停止服务器。也可以通过执行mysqladmin shutdown手动停止服务器。

10.创建配置文件

vim /etc/my.cnf

添加如下配置

[mysql]
prompt=(\u@\h)[\d]>\

[mysqld]
port = 3306
#datadir = /usr/local/mysql/data/
#log_error = error.log
#skip-grant-tables

11.防火墙配置

  1. 查看防火墙状态
sudo ufw status
  1. 开启3306端口
sudo ufw allow 3306
  1. 阿里云配置安全组

developer.aliyun.com/article/767…

配置完成。