You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Windows系统中修改MySQL 8.0命令行客户端的端口号?

How to Change MySQL 8.0 Command-Line Client Port on Windows

Hey there! Let's break down two common scenarios for adjusting the port your MySQL 8.0 command-line client uses on Windows—one for one-time connections, and another for setting a permanent default that sticks around.

1. Temporary Connection with a Custom Port

If you just need to connect using a non-default port once (no config edits required), use the uppercase -P flag when launching the client. This is great for testing or connecting to a MySQL instance running on an alternative port.

Open Command Prompt or PowerShell, then run:

mysql -u your_username -p -P 3307
  • -u your_username: Swap this with your actual MySQL username
  • -p: Triggers a password prompt (don’t mix this up with lowercase -p—uppercase -P is exclusively for specifying the port!)
  • -P 3307: Replace 3307 with the port number your MySQL server is configured to use

2. Permanently Set Default Port for the Client

If you want the command-line client to always use a specific port by default, you’ll need to tweak MySQL’s configuration file:

Step 1: Locate the my.ini Configuration File

MySQL stores its core config in my.ini, usually in this hidden system folder:
C:\ProgramData\MySQL\MySQL Server 8.0\my.ini

If you can’t find it:

  • Open the MySQL command-line client (using the default port first if needed)
  • Run this SQL command to find your data directory (the config file lives nearby):
    SHOW VARIABLES LIKE 'datadir';
    
  • Or, open the Services app (press Win+R, type services.msc), find MySQL80, right-click > Properties, and check the "Path to executable" field—it will include the full path to my.ini.

Step 2: Edit my.ini with Admin Rights

Right-click your text editor (Notepad, VS Code, etc.) and select "Run as administrator"—you need elevated privileges to save changes to this system file.

Find the [client] section and add or modify the port line:

[client]
port=3307
# Keep any existing settings here (like user or password if you’ve set them)

Critical Note: Ensure the [mysqld] section of the same file uses the exact same port number. If your MySQL server is running on a different port, the client won’t be able to connect. Update this line too:

[mysqld]
port=3307
# Leave other server configuration settings as-is

Step 3: Restart MySQL Service

For the changes to take effect, restart the MySQL service:

  1. Open the Services app (Win+R > services.msc)
  2. Locate MySQL80 in the list of services
  3. Right-click > Restart

Step 4: Verify the Change

Open Command Prompt/PowerShell and run:

mysql -u your_username -p

You should connect automatically using your new port. To confirm, run this SQL command in the client:

SHOW VARIABLES LIKE 'port';

It should display your chosen port number.

Pro Tips & Warnings

  • Port Availability: Make sure the port you pick isn’t being used by another program (like a second MySQL instance or Apache). Check port usage with netstat -ano | findstr :3307 in Command Prompt.
  • Syntax Errors: A typo in my.ini can prevent MySQL from starting. If the service fails to restart, double-check your edits for missing characters or typos.
  • Hidden Folders: If you can’t see ProgramData, go to File Explorer > View > check the "Hidden items" box.

内容的提问来源于stack exchange,提问作者Shivam

火山引擎 最新活动