温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL连接字符串怎么写

发布时间:2026-10-19 08:06:00 来源:亿速云 阅读:85 作者:小樊 栏目:数据库

MySQL 连接字符串(Connection String)的写法取决于你使用的编程语言 / 驱动。下面给你常见几种示例:


一、JDBC(Java)

jdbc:mysql://主机名:端口/数据库名?参数

示例:

jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC&characterEncoding=utf8

常用参数:

  • user=root
  • password=123456
  • useUnicode=true
  • autoReconnect=true

二、Python

1️⃣ pymysql

import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='123456',
    database='testdb',
    charset='utf8mb4'
)

2️⃣ SQLAlchemy

mysql+pymysql://user:password@host:port/dbname

示例:

mysql+pymysql://root:123456@localhost:3306/testdb

三、PHP

MySQLi

$conn = new mysqli("localhost", "root", "123456", "testdb");

PDO

new PDO("mysql:host=localhost;dbname=testdb;charset=utf8mb4", "root", "123456");

四、Node.js(mysql2)

mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'testdb'
});

或 URL 形式:

mysql://user:password@host:port/database

五、.NET / C#

Server=localhost;Port=3306;Database=testdb;User=root;Password=123456;CharSet=utf8mb4;

六、通用格式(URL 风格)

mysql://用户名:密码@主机:端口/数据库名?参数

示例:

mysql://root:123456@127.0.0.1:3306/testdb

如果你告诉我:

  • 使用的语言
  • 是否远程连接
  • 是否使用 SSL

我可以帮你写一条准确可用的连接字符串。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手