【MySQL】如何使用Named Pipe协议(Windows)连接MySQL数据库

文章目录

  • 【MySQL】如何使用Named Pipe协议(Windows)连接MySQL数据库
    • 连接MySQL的协议
    • 使用Named Pipe协议(Windows)连接MySQL
      • 步骤1:确认MySQL服务器已启用Named Pipe连接
        • 启动Named Pipe连接方法
      • 步骤2:客户端使用Named Pipe连接MySQL服务器。
        • 例1:客户端使用Named Pipe连接MySQL服务器
        • 例2:客户端指定Socket使用Named Pipe连接MySQL服务器
      • 常见错误
    • 小结

MySQL入门02:关于MySQL连接的ABC
【MySQL】如何使用Named Pipe协议(Windows)连接MySQL数据库
【MySQL】如何使用Shared-memory协议(Windows)连接MySQL数据库
【MySQL】如何使用Unix Sockets 协议连接MySQL数据库

【MySQL】如何使用Named Pipe协议(Windows)连接MySQL数据库

连接MySQL的协议

在MySQL中可以通过指定 --protocol参数,选择连接MySQL的协议。

连接协议(Connection Protocals):

--protocol={TCP|SOCKET|PIPE|MEMORY}

TCP/IP  (ALL)  
	-Transmission Control Protocal/Internet Protocal
	-Connection:local & remote
	-Supports Clasic & X protocol
Socket  file (Unix including Linux/Mac) 
	-Connection:local 
	-Supports Clasic & X protocol
Named Pipe    (Win)  
	-Connection:local 
	-Supports Clasic
Shared Memory (Win)  
	-Connection:local 
	-Supports Clasic

参考:
https://dev.mysql.com/doc/refman/8.0/en/connection-options.html#option_general_protocol
>4.2.3 Command Options for Connecting to the Server

使用Named Pipe协议(Windows)连接MySQL

在Windows环境中,和Unix Sockets连接MySQL相似,可以用Named Pipe协议连接MySQL。
下面介绍如何使用Named Pipe连接MySQL的步骤。

步骤1:确认MySQL服务器已启用Named Pipe连接

查看是否启用Named Pipe连接。

mysql> SHOW GLOBAL VARIABLES LIKE 'named_pipe';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| named_pipe    | OFF   |
+---------------+-------+
1 row in set, 1 warning (0.04 sec)

mysql>

如果值是ON,则表示启用;OFF表示禁用状态。

启动Named Pipe连接方法
  1. 打开配置文件my.ini,将enable-named-pipe变量和socket变量前的注释去掉并保存。

例:

# The next three options are mutually exclusive to SERVER_PORT below.
# skip-networking
enable-named-pipe
# shared-memory

# shared-memory-base-name=MYSQL

# The Pipe the MySQL Server will use.
socket=MYSQL

※ my.ini文件的位置可以通过【mysql --help】命令中的Default options内容查看。
例:

Default options are read from the following files in the given order:
C:\windows\my.ini C:\windows\my.cnf C:\my.ini C:\my.cnf E:\Soft\MySQL8.0\my.ini E:\Soft\MySQL8.0\my.cnf```
  1. 重新启动MySQL服务器。
net stop <MySQL服务名>
net start <MySQL服务名>

例:

C:\Users\Administrator>net stop mysql80
MySQL80 服务正在停止.
MySQL80 服务已成功停止。

C:\Users\Administrator>net start mysql80
MySQL80 服务正在启动 ..
MySQL80 服务已经启动成功。

3.查看Named Pipe连接启用状态。

C:\Users\Administrator>mysql  -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> SHOW GLOBAL VARIABLES LIKE 'named_pipe';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| named_pipe    | ON    |
+---------------+-------+
1 row in set, 1 warning (0.01 sec)

mysql> SHOW  VARIABLES LIKE 'socket';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| socket        | MYSQL |
+---------------+-------+
1 row in set, 1 warning (0.01 sec)

mysql> exit
Bye

步骤2:客户端使用Named Pipe连接MySQL服务器。

例1:客户端使用Named Pipe连接MySQL服务器

通过指定–protocol=pipe,客户端可以使用Named Pipe连接MySQL服务器。

C:\Users\Administrator>mysql --protocol=pipe -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> \s
--------------
mysql  Ver 8.0.28 for Win64 on x86_64 (MySQL Community Server - GPL)

Connection id:          10
Current database:
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         8.0.28 MySQL Community Server - GPL
Protocol version:       10
Connection:             Named pipe: MySQL
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    gbk
Conn.  characterset:    gbk
UNIX socket:            MySQL
Binary data as:         Hexadecimal
Uptime:                 7 min 22 sec

Threads: 2  Questions: 16  Slow queries: 0  Opens: 137  Flush tables: 3  Open tables: 56  Queries per second avg: 0.036
--------------

mysql>

不指定Socket的时候,会使用默认的Named pipe: MySQL。

例2:客户端指定Socket使用Named Pipe连接MySQL服务器
C:\Users\Administrator>mysql --protocol=pipe -u root -p --socket=MYSQL
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> \s
--------------
mysql  Ver 8.0.28 for Win64 on x86_64 (MySQL Community Server - GPL)

Connection id:          11
Current database:
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         8.0.28 MySQL Community Server - GPL
Protocol version:       10
Connection:             Named pipe: MYSQL
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    gbk
Conn.  characterset:    gbk
UNIX socket:            MYSQL
Binary data as:         Hexadecimal
Uptime:                 13 min 4 sec

Threads: 2  Questions: 25  Slow queries: 0  Opens: 138  Flush tables: 3  Open tables: 57  Queries per second avg: 0.031
--------------

mysql> exit
Bye

常见错误

当服务器Named Pipe连接未启用的时候,使用Named Pipe连接会报如下的错误:

C:\Users\Administrator>mysql --protocol=pipe -u root -prootroot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2017 (HY000): Can't open named pipe to host: .  pipe: MySQL (2)

可参考如上的方法在服务器端进行相应的配置。

小结

本文介绍了在Windows环境中,如何使用Named Pipe协议连接MySQL数据库的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在WinForm中连接MySQL数据库,需要使用MySQL连接器/驱动程序。以下是连接WinForm和MySQL数据库的步骤: 1. 下载MySQL连接器/驱动程序:您可以从MySQL官方网站下载MySQL Connector/NET。 2. 将MySQL连接器/驱动程序添加到项目中:将MySQL连接器/驱动程序添加到WinForm项目中。您可以通过NuGet包管理器来添加MySQL连接器/驱动程序。 3. 在WinForm中添加连接字符串:添加连接MySQL数据库所需的连接字符串。连接字符串包括MySQL服务器名称、用户名、密码、数据库名称等信息。 4. 创建连接对象:在WinForm中创建MySQL连接对象,并使用连接字符串初始化它。 5. 打开连接使用连接对象打开与MySQL数据库连接。 6. 执行SQL查询:在WinForm中执行需要执行的SQL查询,如SELECT、INSERT、UPDATE和DELETE语句。 7. 关闭连接使用连接对象关闭与MySQL数据库连接。 以下是一个连接WinForm和MySQL数据库的示例代码: ```csharp using System; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WinFormMySQLConnection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnConnect_Click(object sender, EventArgs e) { // 添加连接字符串 string connectionString = "server=localhost;database=testdb;uid=root;password=123456"; // 创建连接对象 MySqlConnection connection = new MySqlConnection(connectionString); try { // 打开连接 connection.Open(); // 执行SQL查询 MySqlCommand command = new MySqlCommand("SELECT * FROM users", connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理查询结果 Console.WriteLine(reader["id"] + " " + reader["name"]); } // 关闭连接 reader.Close(); connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } ``` 在此示例中,我们通过单击按钮连接MySQL数据库,并执行SELECT语句来检索用户表中的数据。请注意,此示例仅用于演示目的。在实际应用程序中,您需要更多的错误处理和安全性检查。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SQLplusDB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值