MySQL 二进制安装
MySQL官方下载页面:https://downloads.mysql.com/archives/community/
MySQL 5.7.26 二进制安装
1. 下载安装包
root@n37-081-120:~# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
root@n37-081-120:~# mkdir -p /server/tools
root@n37-081-120:~# cd /server/tools/
2. 解压安装包
root@n37-081-120:/server/tools# mv /root/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz .
root@n37-081-120:/server/tools# tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
root@n37-081-120:/server/tools# mkdir /application
root@n37-081-120:/server/tools# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql
3. 设置环境变量
root@n37-081-120:/application/mysql# vim /etc/profile
export PATH=/application/mysql/bin:$PATH
root@n37-081-120:/application/mysql# source /etc/profile
root@n37-081-120:/application/mysql# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
4. 授权
root@n37-081-120:/application/mysql# chown -R mysql.mysql /application/*
root@n37-081-120:/application/mysql# chown -R mysql.mysql /data00
5. 初始化数据(创建系统数据)
root@n37-081-120:/application/mysql# mkdir -p /data00/mysql/data
root@n37-081-120:/application/mysql# mysqld --initialize --user=mysql --basedir=/application/mysql/ --datadir=/data00/mysql/data/
2025-11-25T03:38:31.266035Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-11-25T03:38:31.579191Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-11-25T03:38:31.625107Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-11-25T03:38:31.685240Z 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: 36a76435-c9b0-11f0-8fba-00163e4279f6.
2025-11-25T03:38:31.687033Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-11-25T03:38:31.687839Z 1 [Note] A temporary password is generated for root@localhost: W-x7lo.ls9NJ
--initialize参数:
a. 对于密码复杂度进行定制:12位,4种
b. 密码过期时间:180
c. 给root@localhost用户设置临时密码
--initialize-insecure参数:
无限制,无临时密码
root@n37-081-120:/application/mysql# rm -rf /data00/mysql/data/*
root@n37-081-120:/application/mysql# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/data00/mysql/data/
2025-11-25T03:51:07.251733Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-11-25T03:51:07.534333Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-11-25T03:51:07.586604Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-11-25T03:51:07.595714Z 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: f9362cf0-c9b1-11f0-a645-00163e4279f6.
2025-11-25T03:51:07.597385Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-11-25T03:51:07.597919Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
root@n37-081-120:/application/mysql# ll /data00/mysql/data/
total 110620
-rw-r----- 1 mysql mysql 56 Nov 25 11:51 auto.cnf
-rw-r----- 1 mysql mysql 419 Nov 25 11:51 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Nov 25 11:51 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile1
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 mysql
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 performance_schema
drwxr-x--- 2 mysql mysql 12288 Nov 25 11:51 sys
6. 配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data00/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
7. 启动数据库
a. sys-v方式:
root@n37-081-120:/application/mysql# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
root@n37-081-120:/application/mysql# /etc/init.d/mysqld restart
[ ok ] Restarting mysqld (via systemctl): mysqld.service.
b. 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=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
root@n37-081-120:/application/mysql# systemctl start mysqld.service
root@n37-081-120:/application/mysql# systemctl status mysqld.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/init.d/mysqld; generated; vendor preset: enabled)
Active: active (running) since Tue 2025-11-25 14:04:40 CST; 7s ago
Docs: man:systemd-sysv-generator(8)
Process: 422590 ExecStop=/etc/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 440511 ExecStart=/etc/init.d/mysqld start (code=exited, status=0/SUCCESS)
Tasks: 28 (limit: 4915)
Memory: 173.5M
CPU: 494ms
CGroup: /system.slice/mysqld.service
├─440531 /bin/sh /application/mysql/bin/mysqld_safe --datadir=/data00/mysql/data --pid-file=/data00/mysql/data/n37-081-120.pi
└─440705 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data00/mysql/data --plugin-dir=/application/my
Nov 25 14:04:39 n37-081-120 systemd[1]: Starting LSB: start and stop MySQL...
Nov 25 14:04:39 n37-081-120 mysqld[440511]: Starting MySQL
Nov 25 14:04:40 n37-081-120 mysqld[440511]: ..
Nov 25 14:04:40 n37-081-120 systemd[1]: Started LSB: start and stop MySQL.
分析MySQL无法启动的原因
例:文件权限问题
root@n37-081-120:/application/mysql# cd /data00/mysql/data/
root@n37-081-120:/data00/mysql/data# ll
total 122924
-rw-r----- 1 mysql mysql 56 Nov 25 11:51 auto.cnf
-rw-r----- 1 mysql mysql 356 Nov 25 13:58 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Nov 25 14:04 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 25 14:04 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Nov 25 14:04 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 mysql
-rw-r----- 1 mysql mysql 10626 Nov 25 14:04 n37-081-120.err
-rw-r----- 1 mysql mysql 7 Nov 25 14:04 n37-081-120.pid
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 performance_schema
drwxr-x--- 2 mysql mysql 12288 Nov 25 11:51 sys
root@n37-081-120:/data00/mysql/data# chown root.root ibdata1
root@n37-081-120:/data00/mysql/data# ll
total 122924
-rw-r----- 1 mysql mysql 56 Nov 25 11:51 auto.cnf
-rw-r----- 1 mysql mysql 356 Nov 25 13:58 ib_buffer_pool
-rw-r----- 1 root root 12582912 Nov 25 14:04 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 25 14:04 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Nov 25 14:04 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 mysql
-rw-r----- 1 mysql mysql 10626 Nov 25 14:04 n37-081-120.err
-rw-r----- 1 mysql mysql 7 Nov 25 14:04 n37-081-120.pid
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 performance_schema
drwxr-x--- 2 mysql mysql 12288 Nov 25 11:51 sys
root@n37-081-120:/data00/mysql/data# systemctl stop mysqld
root@n37-081-120:/data00/mysql/data# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
root@n37-081-120:/data00/mysql/data# /etc/init.d/mysqld restart
[....] Restarting mysqld (via systemctl): mysqld.serviceJob for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
failed!
root@n37-081-120:/data00/mysql/data#
root@n37-081-120:/data00/mysql/data# vim n37-081-120.err

错误日志.png
修复错误
root@n37-081-120:/data00/mysql/data# ll
total 110640
-rw-r----- 1 mysql mysql 56 Nov 25 11:51 auto.cnf
-rw-r----- 1 mysql mysql 307 Nov 25 14:09 ib_buffer_pool
-rw-r----- 1 root root 12582912 Nov 25 14:09 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 25 14:09 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile1
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 mysql
-rw-r----- 1 mysql mysql 19521 Nov 25 14:09 n37-081-120.err
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 performance_schema
drwxr-x--- 2 mysql mysql 12288 Nov 25 11:51 sys
root@n37-081-120:/data00/mysql/data# chown -R mysql. *
root@n37-081-120:/data00/mysql/data# ll
total 110640
-rw-r----- 1 mysql mysql 56 Nov 25 11:51 auto.cnf
-rw-r----- 1 mysql mysql 307 Nov 25 14:09 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Nov 25 14:09 ibdata1
-rw-r----- 1 mysql mysql 50331648 Nov 25 14:09 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 25 11:51 ib_logfile1
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 mysql
-rw-r----- 1 mysql mysql 19521 Nov 25 14:09 n37-081-120.err
drwxr-x--- 2 mysql mysql 4096 Nov 25 11:51 performance_schema
drwxr-x--- 2 mysql mysql 12288 Nov 25 11:51 sys
root@n37-081-120:/data00/mysql/data# /etc/init.d/mysqld start
[ ok ] Starting mysqld (via systemctl): mysqld.service.
root@n37-081-120:/data00/mysql/data# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 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>
查看日志:/data/mysql/data/主机名.err
[ERROR] 上下文
可能情况:
a. /etc/my.cnf 路径不正确
b. /tmp/mysql.sock文件修改过 或 删除过
c. 数据目录权限不是mysql
d. 参数错误
1. 设置密码
root@n37-081-120:/data00/mysql/data# mysqladmin -uroot -p password oldboy123
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@n37-081-120:/data00/mysql/data#
root@n37-081-120:/data00/mysql/data# mysqladmin -uroot -p password 123
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.
2. 管理员用户密码忘记了,怎么处理?
--skip-grant-tables #跳过授权表
--skip-networking #跳过远程登录
1)关闭数据库
root@n37-081-120:/data00/mysql/data# /etc/init.d/mysqld stop
[ ok ] Stopping mysqld (via systemctl): mysqld.service.
2)启动数据到维护模式
root@n37-081-120:/data00/mysql/data# mysqld_safe --skip-grant-tables --skip-networking &
[1] 515157
root@n37-081-120:/data00/mysql/data# 2025-11-25T06:31:30.222955Z mysqld_safe Logging to '/data00/mysql/data/n37-081-120.err'.
2025-11-25T06:31:30.253547Z mysqld_safe Starting mysqld daemon with databases from /data00/mysql/data
root@n37-081-120:/data00/mysql/data# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 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>
3)修改密码
mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by '1';
ERROR 1290 (HY000) : The MySQL server is running with the --skip-grant-tables option so it cannot execute thie statement
# 刷新授权表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
4)重启数据库验证
root@n37-081-120:/data00/mysql/data# /etc/init.d/mysqld restart
[ ok ] Restarting mysqld (via systemctl): mysqld.service.
root@n37-081-120:/data00/mysql/data#
root@n37-081-120:/data00/mysql/data# mysql -uroot -p1
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.26 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>