Connection string mysql
By FoxLearn 11/18/2024 6:24:29 AM 28
MySQL Connector .Net
The MySQL Connector/Net is a .NET driver that allows you to connect to a MySQL database from C# or other .NET-based applications.
Standard connection string mysql
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Specifying TCP port connection string mysql
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Parameters:
myUsername
: The MySQL user you're logging in as (e.g.,root
).myPassword
: The password for the given MySQL user.myServerAddress
: The address of the MySQL server (e.g.,localhost
, an IP address, or a domain name).myPort
: The port on which MySQL is running (default is3306
).myDataBase
: The name of the database you wish to connect to.
Localhost Connection string mysql (default port 3306)
Server=localhost;Database=my_database;Uid=root;Pwd=my_password;
Remote Server connection string mysql (with custom port)
Server=192.168.1.10;Port=3307;Database=my_database;Uid=my_user;Pwd=my_password;
Multiple servers
Use this to connect to a server in a replicated server configuration without concern on which server to use.
Server=serverAddress1,serverAddress2,serverAddress3;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Using encryption
Use SSL if the server supports it, but allow connection in all cases
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;SslMode=Preferred;
SSL with a file-based certificate
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;SSL Mode=Required;CertificateFile=C:\folder\client.pfx;CertificatePassword=pass;
SSL with a personal store-based certificate
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;SSL Mode=Required;Certificate Store Location=CurrentUser;
SslMode: Controls SSL/TLS encryption for secure connections. Possible values are:
None
(no SSL)Preferred
(use SSL if available)Required
(use SSL, otherwise fail)VerifyCA
(verify the certificate authority)VerifyFull
(verify the certificate and the host name)
Connection Pooling
By default, MySQL Connector/NET uses connection pooling. You can disable it or adjust the pool size.
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Pooling=True;MinPoolSize=5;MaxPoolSize=100;
How to use the Connection String in C#?
string con = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Connection Timeout=30;"; try { using (MySqlConnection conn = new MySqlConnection(con)) { conn.Open(); //Connection successful, perform database operations here } } catch (Exception ex) { throw ex; }
When connecting to a MySQL database in a C# application, ensure that the connection string matches the configuration of your MySQL server and includes all required authentication and connection options for your use case.