MySQL安装手册

MySQL离线安装

安装版本:5.7.38

离线安装包下载地址:https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

1. 创建mysql用户

useradd -s /sbin/nologin -M mysql

2. 创建安装目录

mkdir /data/

3. 上传离线安装包,解压至/data目录下,重命名和创建相关目录

tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /data/
cd /data/
mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
mkdir -p /data/mysql/{etc,logs,tmp,data}

4. 编辑配置文件

    # vim /data/mysql/etc/my.cnf
[mysqld]
daemonize = on
user = mysql
port = 3306
character-set-server = utf8
collation-server = utf8_general_ci
basedir = /data/mysql/
datadir = /data/mysql/data/
log-error = /data/mysql/logs/mysqld.log
socket = /data/mysql/tmp/mysql.sock
bind-address = 0.0.0.0
pid-file = /data/mysql/tmp/mysqld.pid
max_connections = 2000

[client]
default-character-set = utf8
source  /data/mysql/etc/my.cnf

5. 修改目录所属人所属组

chown mysql:mysql -R /data/mysql

6. 初始化数据库

/data/mysql/bin/mysqld --initialize --user=mysql --basedir=/data/mysql/ --datadir=/data/mysql/data/

备注说明:--initialize表示默认生成一个安全的密码,请记住该密码,之后需要使用。

7. 为MySQL配置systemctl启动

# vim /usr/lib/systemd/system/mysqld.service
Description=MySQL GZ Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
ExecStart=/data/mysql/bin/mysqld --defaults-file=/data/mysql/etc/my.cnf
LimitNOFILE = 5000

启动MySQL

systemctl start mysqld.service

设置MySQL开机自启

systemctl enable mysqld.service

8. 修改密码并设置远程root用户登录

/data/mysql/bin/mysql -h 127.0.0.1 -u root -p
回车输入之前初始化时生成的密码以登录


修改密码(password为新密码,请自行修改)
mysql> alter user 'root'@'localhost' identified by 'password';
mysql> flush privileges;


创建root远程连接账户(password为新密码,请自行修改)
mysql> GRANT ALL PRIVILEGES ON *.* TO `root`@`%` IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> flush privileges;

9. 防火墙放行3306端口

firewall-cmd --add-port=3306/tcp
firewall-cmd --add-port=3306/tcp --permanent

你可能感兴趣的:(MySQL安装手册)