MySQL使用教程笔记(MySQL Workbench远程访问和C#远程访问)

远程连接MySQL数据库_mysql远程访问数据库-CSDN博客

如何远程连接mysql数据库服务器_mysql如何连接远程数据库服务器_如何连接远程数据库mysql - 腾讯云开发者社区 - 腾讯云 (tencent.com)

如何使用C#连接远程MySQL服务器?_webrtc_weixin_0010034-MySQL数据库 (csdn.net)

c#中mysql远程连接方法及实例_c#连接远程mysql-CSDN博客

一,设置远程访问:

1,前提:关闭防火墙或开放3306端口

关闭防火墙的操作

  控制面板》Windows防火墙》关闭防火墙

防火墙开启,开放3306端口的操作:

1,允许的应用》如果列表中没有》就手动添加mysql.exe

 

2,返回到 “Windows Defender 防火墙” 窗口。
3,点击左侧的 “高级设置”。这将打开 “Windows Defender 防火墙和高级安全性” 窗口。
4,在左侧栏中,选择 “入站规则”。
5,在右侧栏中,选择 “新建规则…”。

配置新规则:

  1,规则类型:
    选择 “端口”,然后点击 “下一步”。
  2,协议和端口
    选择 “TCP”。
    选择 “特定本地端口”,然后输入 3306。
    点击 “下一步”。
  3,操作:
    选择 “允许连接”,然后点击 “下一步”。
  4,配置文件:
    选择 “域”、“专用”和“公用”,然后点击 “下一步”。
  5,名称:
    为规则命名,例如 MySQL 3306。
    点击 “完成”。
确认规则生效
  1,在 “入站规则” 列表中,找到你刚刚创建的 MySQL 3306 规则,并确保它处于启用状态。
  2,同样地,你可以重复上述步骤在 “出站规则” 中创建一条允许 3306 端口的规则,确保双向通信畅通。
  完成这些步骤后,Windows 防火墙将允许 MySQL 使用 3306 端口进行连接


原文链接:https://blog.csdn.net/chenhao0568/article/details/139293788

2,设置mysql远程连接权限

登录进入mysql控制台
cd D:\MySQL\mysql-8.0.32-winx64\bin

mysql -h localhost -u root -p
跳出密码输入界面,Enter password: ******

show databases;

use mysql;

查询user和host的权限
select user , host from user;

%为任意访问地址
update user set host = '%' where user = 'root';

刷新权限,权限更新后,刷新才会起作用
flush privileges;

 3,可以用MySQL Wrokbench远程访问了

 4,C# 远程访问代码:

    public static class SqlSugarHelper_MES
    {
        //连接字符串
        public static string ConnectionString = string.Empty;
        //数据库类型
        public static DbType DBType = DbType.SqlServer;

        public static SqlSugarClient SqlSugarClient
        {
            get
            {
                return new SqlSugarClient(new ConnectionConfig()
                {

                    ConnectionString = ConnectionString,//必填, 数据库连接字符串
                    //DbType = DbType.Sqlite,//必填,选择数据库类型                    
                    //DbType = DbType.SqlServer ,//必填,选择数据库类型 
                    DbType = DBType,
                    IsAutoCloseConnection = true,//设置为true无需使用using或者Close操作,自动关闭连接,不需要手动关闭数据链接
                    InitKeyType = InitKeyType.Attribute

                });
            }
        }
    }

    /// <summary>
    /// 设置数据库的连接字符串
    /// </summary>
    public static class SqlSugarService_MES
    {
        /// <summary>
        /// 设置数据库的类型
        /// </summary>
        /// <param name="dbType"></param>
        public static void SetDBType(string dbType)
        {
            switch (dbType.ToLower())
            {
                case "sqlserver":
                    SqlSugarHelper_MES.DBType = DbType.SqlServer;
                    break;
                case "sqlite":
                    SqlSugarHelper_MES.DBType = DbType.Sqlite;
                    break;
                case "mysql":
                    SqlSugarHelper_MES.DBType = DbType.MySql;
                    break;
                default:
                    SqlSugarHelper_MES.DBType = DbType.SqlServer;
                    break;
            }
        }

        /// <summary>
        /// 设置数据库的连接字符串
        /// </summary>
        /// <param name="ConnectionString"></param>
        public static void SetConnectionString(string ConnectionString)
        {
            SqlSugarHelper_MES.ConnectionString = ConnectionString;
        }
    }
}
            if (GlobalVariable.myParameter.IsSendData & GlobalVariable.myParameter.IsStandardSend)
            {
                if (GlobalVariable.myParameter.APIType == "MySQL")
                {
                    //设置MES服务器的数据库类型和连接字符串
                    SqlSugarService_MES.SetDBType(GlobalVariable.myParameter.APIType);
                    SqlSugarService_MES.SetConnectionString(GlobalVariable.myParameter.APIUrl);
                    //Database = tighteningresultdb; Server = 192.168.0.106; User Id = root; Password = 123456; pooling = false; CharSet = utf8; port = 3306; SSL Mode = None; allowPublicKeyRetrieval = true

                    //【初始化MES服务器的数据表,如果没有数据表,就新建一张表】
                    SqlSugarHelper_MES.SqlSugarClient.CodeFirst.SplitTables().InitTables<TighteningResult>();

                }
            }

  

参考学习笔记:https://blog.csdn.net/qq_39653954/article/details/125616870

1,联合唯一索引

例如:t_aa 表中有aa,bb两个字段,如果不希望有2条一模一样的记录(即:aa字段的值可以重复; bb字段的值也可以重复,但是一条记录(aa,bb)组合值不允许重复),需要给 t_aa 表添加多个字段的联合唯一索引:

alter table t_aa add unique index(aa,bb);

  

posted @ 2024-08-13 21:58  包子789654  阅读(3)  评论(0编辑  收藏  举报