MySQL端口号,-MySQL修改端口号图文教程:从配置到验证 - important
MySQL修改端口号图文教程:从配置到验证
引言1. 修改端口前的准备工作1.1 查看当前MySQL端口1.2 检查新端口是否可用
2. 修改MySQL配置文件2.1 找到MySQL配置文件2.2 备份配置文件2.3 修改配置文件2.4 配置文件详解
3. 重启MySQL服务3.1 Linux系统3.2 Windows系统3.3 macOS系统
4. 验证端口修改4.1 检查服务监听端口4.2 登录验证4.3 测试旧端口是否失效
5. 客户端连接配置修改5.1 命令行连接5.2 Java应用配置5.3 Python应用配置5.4 PHP应用配置5.5 Navicat/DBeaver等图形工具
6. 防火墙配置6.1 Linux防火墙(iptables)6.2 Linux防火墙(firewalld)6.3 Windows防火墙
7. 常见问题排查7.1 MySQL启动失败7.2 配置文件权限问题7.3 SELinux阻止(CentOS/RHEL)
8. 多实例配置示例9. 安全建议9.1 限制连接IP9.2 创建专用用户
总结 拼多多帮助力入口
The Begin点点关注,收藏不迷路
引言
MySQL默认使用3306端口,但在某些场景下我们需要修改这个默认端口:
端口冲突:3306端口被其他应用程序占用安全考虑:修改默认端口可以降低被自动化攻击的风险多实例运行:一台服务器运行多个MySQL实例需要不同端口
本文将详细介绍如何修改MySQL的端口号,包括配置文件修改、服务重启和验证方法。
1. 修改端口前的准备工作
1.1 查看当前MySQL端口
在修改之前,先确认当前MySQL使用的端口:
# 方法1:登录MySQL查看
mysql -u root -p
mysql>SHOW VARIABLES LIKE 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
# 方法2:使用netstat查看
netstat -tlnp | grep mysql
# 或
ss -tlnp | grep mysql
1.2 检查新端口是否可用
确保要修改的新端口没有被占用:
# 检查5350端口是否被占用
netstat -tlnp | grep 5350
# 如果没有输出,说明端口可用
2. 修改MySQL配置文件
2.1 找到MySQL配置文件
MySQL的配置文件通常位于以下位置之一:
操作系统配置文件路径Linux/etc/mysql/my.cnf 或 /etc/my.cnfWindowsC:\ProgramData\MySQL\MySQL Server X.X\my.inimacOS/usr/local/mysql/etc/my.cnf
2.2 备份配置文件
修改前建议先备份配置文件:
# Linux/macOS
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak
# Windows(管理员CMD)
copy "C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" "C:\ProgramData\MySQL\MySQL Server 8.0\my.ini.bak"
2.3 修改配置文件
使用vim编辑配置文件(Linux/macOS):
sudo vim /etc/mysql/my.cnf
在[mysqld]部分添加或修改端口配置:
[mysqld]
# 修改端口号为5350
port = 5350
# 其他配置保持不变...
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
Windows系统修改my.ini:
[mysqld]
# 修改端口号为5350
port=5350
# 其他配置...
basedir=C:/Program Files/MySQL/MySQL Server 8.0/
datadir=C:/ProgramData/MySQL/MySQL Server 8.0/Data
2.4 配置文件详解
[mysqld]
# 监听的端口号(默认3306)
port = 5350
# 绑定IP地址,0.0.0.0表示监听所有网卡
bind-address = 0.0.0.0
# 如果只想本地访问,可以设置为
# bind-address = 127.0.0.1
# 其他常用配置
max_connections = 500
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
3. 重启MySQL服务
修改配置文件后,需要重启MySQL服务使配置生效:
3.1 Linux系统
# 使用systemd(主流Linux发行版)
sudo systemctl restart mysql
# 或
sudo systemctl restart mysqld
# 使用service命令
sudo service mysql restart
# 查看服务状态
sudo systemctl status mysql
3.2 Windows系统
# 以管理员身份运行CMD
# 停止MySQL服务
net stop MySQL84
# 启动MySQL服务
net start MySQL84
# 或者在服务管理器中重启
# 按Win + R,输入 services.msc
# 找到MySQL服务,右键点击"重启"
3.3 macOS系统
# 使用brew安装的MySQL
brew services restart mysql
# 使用官方安装包
sudo /usr/local/mysql/support-files/mysql.server restart
4. 验证端口修改
4.1 检查服务监听端口
# Linux/macOS
netstat -tlnp | grep 5350
# 或
ss -tlnp | grep 5350
# 输出示例
tcp6 0 0 :::5350 :::* LISTEN 12345/mysqld
4.2 登录验证
# 连接时需要指定新端口
mysql -u root -p -P 5350
# 如果连接成功,进入MySQL后查看端口
mysql>SHOW VARIABLES LIKE 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 5350 |
+---------------+-------+
1 row in set (0.00 sec)
4.3 测试旧端口是否失效
# 使用旧端口3306连接应该失败
mysql -u root -p -P 3306
# 输出: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
5. 客户端连接配置修改
修改端口后,所有连接MySQL的应用也需要相应调整:
5.1 命令行连接
# 方式1:使用-P参数指定端口
mysql -h localhost -u root -p -P 5350
# 方式2:在连接字符串中指定
mysql --host=localhost --port=5350 --user=root --password
5.2 Java应用配置
# application.properties
spring.datasource.url=jdbc:mysql://localhost:5350/mydb?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:5350/mydb?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
5.3 Python应用配置
import pymysql
# 连接MySQL
connection = pymysql.connect(
host='localhost',
port=5350, # 指定新端口
user='root',
password='123456',
database='mydb'
)
5.4 PHP应用配置
// PDO连接
$dsn = 'mysql:host=localhost;port=5350;dbname=mydb;charset=utf8mb4';
$username = 'root';
$password = '123456';
try {
$pdo = new PDO($dsn, $username, $password);
echo "连接成功";
} catch (PDOException $e) {
echo "连接失败: " . $e->getMessage();
}
5.5 Navicat/DBeaver等图形工具
在创建连接时,修改端口号为5350:
#mermaid-svg-2fIBLLCzJVPVuX64{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2fIBLLCzJVPVuX64 .error-icon{fill:#552222;}#mermaid-svg-2fIBLLCzJVPVuX64 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2fIBLLCzJVPVuX64 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2fIBLLCzJVPVuX64 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2fIBLLCzJVPVuX64 .marker.cross{stroke:#333333;}#mermaid-svg-2fIBLLCzJVPVuX64 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2fIBLLCzJVPVuX64 p{margin:0;}#mermaid-svg-2fIBLLCzJVPVuX64 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster-label text{fill:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster-label span{color:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster-label span p{background-color:transparent;}#mermaid-svg-2fIBLLCzJVPVuX64 .label text,#mermaid-svg-2fIBLLCzJVPVuX64 span{fill:#333;color:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 .node rect,#mermaid-svg-2fIBLLCzJVPVuX64 .node circle,#mermaid-svg-2fIBLLCzJVPVuX64 .node ellipse,#mermaid-svg-2fIBLLCzJVPVuX64 .node polygon,#mermaid-svg-2fIBLLCzJVPVuX64 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2fIBLLCzJVPVuX64 .rough-node .label text,#mermaid-svg-2fIBLLCzJVPVuX64 .node .label text,#mermaid-svg-2fIBLLCzJVPVuX64 .image-shape .label,#mermaid-svg-2fIBLLCzJVPVuX64 .icon-shape .label{text-anchor:middle;}#mermaid-svg-2fIBLLCzJVPVuX64 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2fIBLLCzJVPVuX64 .rough-node .label,#mermaid-svg-2fIBLLCzJVPVuX64 .node .label,#mermaid-svg-2fIBLLCzJVPVuX64 .image-shape .label,#mermaid-svg-2fIBLLCzJVPVuX64 .icon-shape .label{text-align:center;}#mermaid-svg-2fIBLLCzJVPVuX64 .node.clickable{cursor:pointer;}#mermaid-svg-2fIBLLCzJVPVuX64 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2fIBLLCzJVPVuX64 .arrowheadPath{fill:#333333;}#mermaid-svg-2fIBLLCzJVPVuX64 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2fIBLLCzJVPVuX64 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2fIBLLCzJVPVuX64 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2fIBLLCzJVPVuX64 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2fIBLLCzJVPVuX64 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2fIBLLCzJVPVuX64 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster text{fill:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 .cluster span{color:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2fIBLLCzJVPVuX64 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2fIBLLCzJVPVuX64 rect.text{fill:none;stroke-width:0;}#mermaid-svg-2fIBLLCzJVPVuX64 .icon-shape,#mermaid-svg-2fIBLLCzJVPVuX64 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2fIBLLCzJVPVuX64 .icon-shape p,#mermaid-svg-2fIBLLCzJVPVuX64 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2fIBLLCzJVPVuX64 .icon-shape rect,#mermaid-svg-2fIBLLCzJVPVuX64 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2fIBLLCzJVPVuX64 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2fIBLLCzJVPVuX64 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2fIBLLCzJVPVuX64 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
新建连接
主机: localhost
端口: 5350
用户名: root
密码: ******
测试连接
6. 防火墙配置
如果开启了防火墙,需要放行新端口:
6.1 Linux防火墙(iptables)
# 放行5350端口
sudo iptables -A INPUT -p tcp --dport 5350 -j ACCEPT
# 保存规则
sudo service iptables save
6.2 Linux防火墙(firewalld)
# 放行5350端口
sudo firewall-cmd --permanent --add-port=5350/tcp
sudo firewall-cmd --reload
# 查看已放行端口
sudo firewall-cmd --list-ports
6.3 Windows防火墙
# PowerShell管理员模式
New-NetFirewallRule -DisplayName "MySQL 5350" -Direction Inbound -Protocol TCP -LocalPort 5350 -Action Allow
7. 常见问题排查
7.1 MySQL启动失败
# 查看错误日志
sudo tail -f /var/log/mysql/error.log
# 常见错误:端口被占用
# 2024-01-15T10:30:45.123456Z 0 [ERROR] Can't start server: Bind on TCP/IP port: Address already in use
解决方案:
# 检查端口占用
sudo lsof -i :5350
# 杀掉占用进程或更换其他端口
7.2 配置文件权限问题
# 确保MySQL用户可读配置文件
sudo chmod 644 /etc/mysql/my.cnf
sudo chown mysql:mysql /etc/mysql/my.cnf
7.3 SELinux阻止(CentOS/RHEL)
# 查看SELinux状态
getenforce
# 临时禁用(测试用)
setenforce 0
# 永久放行MySQL新端口
semanage port -a -t mysqld_port_t -p tcp 5350
8. 多实例配置示例
如果一台服务器运行多个MySQL实例,可以这样配置:
# 实例1: my.cnf
[mysqld]
port = 3306
datadir = /var/lib/mysql1
socket = /var/lib/mysql1/mysql.sock
# 实例2: my2.cnf
[mysqld]
port = 5350
datadir = /var/lib/mysql2
socket = /var/lib/mysql2/mysql.sock
启动多个实例:
# 启动第一个实例
mysqld --defaults-file=/etc/mysql/my.cnf &
# 启动第二个实例
mysqld --defaults-file=/etc/mysql/my2.cnf &
9. 安全建议
修改端口后,建议同时进行以下安全配置:
9.1 限制连接IP
[mysqld]
port = 5350
# 只允许本地连接
bind-address = 127.0.0.1
# 或者允许特定IP
# bind-address = 192.168.1.100
9.2 创建专用用户
-- 创建仅限特定IP连接的用户
CREATE USER 'app_user'@'192.168.1.%' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON mydb.* TO 'app_user'@'192.168.1.%';
FLUSH PRIVILEGES;
总结
修改MySQL端口号的完整流程:
步骤操作命令/说明1检查当前端口SHOW VARIABLES LIKE 'port';2修改配置文件在[mysqld]下添加port=53503重启服务sudo systemctl restart mysql4验证修改netstat -tlnp | grep 53505测试连接mysql -P 5350 -u root -p6更新应用配置修改数据库连接URL7配置防火墙放行5350端口
注意事项:
修改前备份配置文件确保新端口未被占用重启服务前保存所有工作更新所有应用的数据库连接配置
The End点点关注,收藏不迷路