Table of Contents
安装依赖包
yum -y install libaio perl perl-devel libncurses* autoconf numactl
新建用户
useradd mysql
解压
tar xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/
ln -s mysql-5.7.32-linux-glibc2.12-x86_64 mysql
初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data3306
配置文件
touch /usr/local/mysql/data3306/error.log
chown -R mysql:mysql /usr/local/mysql/data3306/
cat > /usr/local/mysql/data3306/my.cnf <<"EOF"
[mysqld]
# ===== 基础配置 =====
basedir = /usr/local/mysql # MySQL安装目录
datadir = /usr/local/mysql/data3306 # 数据目录
socket = /usr/local/mysql/data3306/mysql.sock
pid-file = /usr/local/mysql/data3306/mysqld.pid
user = mysql
port = 3306
bind-address = 0.0.0.0 # 远程访问需改为 0.0.0.0(注意安全)
skip-name-resolve # 禁用DNS解析,提升连接速度
# ===== 字符集配置 =====
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# ===== 日志配置 =====
log_error = /usr/local/mysql/data3306/error.log # 确保logs目录存在
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/data3306/slow.log # 慢查询日志
long_query_time = 2
log_queries_not_using_indexes = 1
# ===== InnoDB 引擎优化 =====
default-storage-engine = InnoDB
innodb_buffer_pool_size = 1G # 根据物理内存调整(建议70-80%)
innodb_log_file_size = 512M # 需重启生效
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1 # 1=严格ACID,2=性能优先
innodb_file_per_table = 1 # 独立表空间
# ===== 连接与线程 =====
max_connections = 500 # 最大连接数
thread_cache_size = 128
wait_timeout = 300 # 非交互连接超时(秒)
interactive_timeout = 300
# ===== 安全与权限 =====
symbolic-links = 0 # 禁用符号链接(安全加固)
local_infile = 0 # 禁用本地文件加载
# ===== 二进制日志(主从复制时启用)=====
server-id = 3306 # 主从唯一ID
log_bin = /usr/local/mysql/data3306/mysql-bin.log
expire_logs_days = 30 # 自动清理binlog
binlog_format = row
# ===== 其他优化 =====
key_buffer_size = 64M
tmp_table_size = 64M
max_heap_table_size = 64M
query_cache_type = 0 # 关闭查询缓存
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error = /usr/local/mysql/data3306/error.log # 与[mysqld]中一致
socket = /usr/local/mysql/data3306/mysql.sock
[client]
socket = /usr/local/mysql/data3306/mysql.sock # 客户端连接socket路径
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
启动服务
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/data3306/my.cnf &
登录MySQL
/usr/local/mysql/bin/mysql -u root -P 3306 -S /usr/local/mysql/data3306/mysql.sock
修改密码
alter user root@localhost identified by 'root';
create user root@'%' identified by 'root';
grant all on *.* to root@'localhost';
flush privileges;
# 方法2:
-- 切换到 mysql 数据库
USE mysql;
-- 直接设置 root 密码(注意 mysql.user 的字段:authentication_string)
UPDATE mysql.user
SET authentication_string = PASSWORD('root'),
plugin = 'mysql_native_password'
WHERE User='root' AND Host='localhost';
-- 刷新权限
FLUSH PRIVILEGES;
# 新建用户 root@127.0.0.1
create user root@'127.0.0.1' identified with mysql_native_password by 'admin_123';
grant all on *.* to root@'127.0.0.1';
停止数据库
mysqladmin -u root shutdown -p -S /usr/local/mysql/data3306/mysql.sock
忘记 root 密码
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/3306/data/my.cnf --skip-grant-tables --skip-networking &
mysql -u root -p
[root@oracleLinux8 data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/3306/data/my.cnf --skip-grant-tables --skip-networking &
[1] 3701
[root@oracleLinux8 data]# 2025-10-01T09:18:45.625339Z mysqld_safe Logging to '/usr/local/mysql/3306/error_log/mysql-error.log'.
2025-10-01T09:18:45.636383Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/3306/data
[root@oracleLinux8 data]#
[root@oracleLinux8 data]# ps -ef | grep mysql
root 3701 1710 0 17:18 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/3306/data/my.cnf --skip-grant-tables --skip-networking
mysql 4141 3701 10 17:18 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/3306/data/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/usr/local/mysql/3306/error_log/mysql-error.log --pid-file=/usr/local/mysql/3306/pid_log/mysql.pid --socket=/usr/local/mysql/3306/socket/mysql.sock --port=3306
root 4180 1710 0 17:18 pts/2 00:00:00 grep --color=auto mysql
[root@oracleLinux8 data]#
[root@oracleLinux8 data]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.4.3 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
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>
mysql>
mysql> \s
--------------
mysql Ver 8.4.3 for Linux on aarch64 (MySQL Community Server - GPL)
Connection id: 7
Current database:
Current user: root@
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.4.3 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /usr/local/mysql/3306/socket/mysql.sock
Binary data as: Hexadecimal
Uptime: 12 sec
Threads: 1 Questions: 6 Slow queries: 0 Opens: 105 Flush tables: 3 Open tables: 26 Queries per second avg: 0.500
--------------
相关环境变量
# socket 相关
export MYSQL_UNIX_PORT=/usr/local/mysql/3306/socket/mysql.sock
配置 prompt
🧰 常用占位符(非常关键)
| 占位符 | 含义 |
|---|---|
\h |
host(服务器) |
\p |
port(端口) |
\d |
database(当前数据库) |
\u |
user(当前用户) |
\_ |
空格 |
\D |
日期时间 |
MYSQL_PS1="[ip:\h][dbname:\d][username:\u][\S] > " mysql -u root -p
[mysql@binghe198 ~]$ MYSQL_PS1="\D:[ip:\h][dbname:\d][username:\u][\S] > " mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.32-log MySQL Community Server
最后修改时间:2025-12-31 22:21:33
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




