# 创建存储软件包目录
mkdir -p /opt/software/mysql && cd /opt/software/mysql
# 创建mysql用户
useradd -r -s /sbin/nologin -c 'MySQL DataBase Server User' mysql
# 上传软件包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
# 解压
tar xf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
# 软连接
ln -s /opt/software/mysql/mysql-5.7.27-linux-glibc2.12-x86_64/ /usr/local/mysql
# 修改文件用户和用户主为mysql
chown -R mysql.mysql /usr/local/mysql/
# 编写环境变量文件 输入 i 进入插入模式
vi /etc/profile.d/mysql.sh
#!bin/bash
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
# 保存退出 :wq
# 配置生效
source /etc/profile.d/mysql.sh
# 检查mysql 命令是否可以执行
root@localhost:~# mysql -V
mysql Ver 14.14 Distrib 5.7.27, for linux-glibc2.12 (x86_64) using EditLine wrapper
# 创建mysql 存储目录 并修改用户和用户组
install -d /data/mysql -o mysql -g mysql
# 初始化mysql
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
root@localhost:~# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql2025-01-03T04:00:02.850645Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-01-03T04:00:03.178500Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-01-03T04:00:03.259141Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-01-03T04:00:03.320039Z 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: 35dcb864-c987-11ef-ab84-52540045874c.
2025-01-03T04:00:03.322324Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-01-03T04:00:03.323656Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
# 创建配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/data/mysql
EOF
# 修改用户和用户主
chown mysql.mysql /etc/my.cnf
# 编写systemd 服务
cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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
# 修改成自己的安装目录
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 65536
Environment=MYSQLD_PARENT_PID=1
EOF
# 启动服务
systemctl start mysqld
# 设置开机自启动
systemctl enable mysqld
# 使用mysql 命令行连接mysql服务端 默认不带密码
root@localhost:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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>
# 设置root密码 为 root 可自行设置密码
mysqladmin -uroot -p password root
root@localhost:~# mysqladmin -uroot -p password root
Enter password: # 这里让我们输入之前的旧密码,可惜呀,我们之前的旧密码为空,因此无需输入,直接回车即可!
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
# 再次登录 可以看到可以正常登录
root@localhost:~# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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>
# 创建一个允许root 远程登陆的账号 '' 里面输入自己的密码即可
CREATE USER root@'%' IDENTIFIED WITH mysql_native_password BY 'root';
# 赋予权限
GRANT ALL PRIVILEGES ON *.* TO root@'%';
FLUSH PRIVILEGES;
# 运行的结果
mysql> CREATE USER root@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
# 输入 exit 或 Ctrl + c 退出 mysql终端
# 测试远程连接
Linux 安装 MySQL 5.7.27
于 2025-02-06 22:29:02 首次发布