Unable connect to MySQL from C# but possible from other apps - c#

I wanted to change MySQL server in my C# app. The app is run in my company and both - old and new MySQL servers are only accessible from the intranet. The problem is I can connect from HeidiSQL software and from python code but not from C#...
I have tried all possible solutions what I found, for ex. disabling firewall, using different packages from NuGet, modifying connection string, I created new console project only to paste various MySQL connection code - always with same error
Message: Unable to connect to any of the specified MySQL hosts.
Source: MySql.Data
Number: 1042
I', using .net Framework 4.5.2 (4.6.1 in my test project) and MySQL Server is '5.6.44-log - MySQL Community Server (GPL)'
One of C# connection code example that I have tested that is NOT working
using MySql.Data.MySqlClient;
public static MySqlConnection DB_connection;
DB_connection = new MySqlConnection(#"Server=MyIP;Database=myDB;Uid=my_user;Pwd=pass;");
try
{
DB_connection.Open();
isConn = true;
}
catch (...)
Working python code run from the same PC
import pymysql
import pprint
connection = pymysql.connect(host='MyIP',
user='my_user',
password='pass',
db='myDB')
try:
with connection.cursor() as cursor:
sql = "select * from table;"
cursor.execute(sql)
# connection.commit()
result = cursor.fetchall()
pprint.pprint(result)
finally:
connection.close()

Don't see anything wrong with your posted connection string but in case your's is a replicated scenario (I mean DB replication exists) then you will have to specify the replicated server IP/hostname as well like
Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;

Related

UWP to MariaDB using MySQL Connector Net 6.7.9

I've been trying to get some initial code working before I start working on my app.
I have had this working maybe a year ago so something tells me there is an update/version issue.
But any help is good so.
I have a raspberry pi set up with apache, MariaDB, php etc etc. I have a simple webpage running so apache is fine, I have another webpage that pulls data from a table so MariaDB is fine (fine-ish, given my issue). I have myphpadmin set up and can log in create new db's and users etc.
Now I would like to have a UWP app interact with a db hosted on my pi. I created a new user with SELECT and INSERT only for a specific db (hopefully this reduces security issues but I'm new so maybe I'm wishing).
I also then found the mariadb config file and commented the bind-address line. The skip-networking line is no longer in the file so just ignored that. (this is what the docs say to allow remote connections).
I have opened port 3306 on my router, and I also have a domain name and use no-ip but I assume this is all fine as apache runs fine. Saw someone talking about SSH, I changed my ssh port but I don't think that would be the issue.
Then the UWP.
Last time I tried this I had to use MySQL Connector Net 6.7.9 as newer versions didn't work with RT. So I have added this to the project references.
Now for simple testing the connection I have a button and TextBlock, click the button to attempt connecting and output the exception/result.
Have tried conn string builder as well as just a straight forward string, neither work.
private void ConnectDatabase_Click(object sender, RoutedEventArgs e)
{
string ConnString;
MySqlConnection Conn;
MySqlConnectionStringBuilder Csb = new MySqlConnectionStringBuilder();
Csb.Server = "http://rnd-domain.me";
Csb.Port = 3306;
Csb.UserID = "usr";
Csb.Password = "passwd";
Csb.Database = "testdb";
ConnString = "server=http://rnd-domain.me;database=testdb;uid=usr;pwd=passwd;";
Conn = new MySqlConnection(ConnString);
try
{
Conn.Open();
DbUpdateText.Text = "connected";
Conn.Close();
}
catch(Exception ex)
{
DbUpdateText.Text = ex.Message;
}
}
When I click the button the ex.Message is 'Unable to connect to any of the specified MySQL hosts.'.
There are no errors thrown with the app, just this unhelpful message.
I've googled this and wasted 80% of my sunday trying to get this working. I've added sslmode=none, charset=utf8, port=3306. I tried using server=localhost or using my current ip and even using my mariadb master user (not root).
What could be the issue?
Thanks.
Edit:
I just used the pi's internal IP and got a new ex.Message, understood this one about SSl, added sslmode=none and all is working.
So now its just getting around the domain name issue?
Your connection string isn't quite right.
You probably want this:
server=rnd-domain.me;database=testdb;uid=usr;pwd=REDACTED;
It doesn't make sense to mention http:// in a MySQL connection string, because the connection doesn't use the HTTP protocol. Rather it uses the MySQL protocol.
The issue was using a pc (client) and raspi (server) both inside the home network.
When I changed the host to the internal IP of the raspi it all worked fine.

C# with MySQL - Error: "Guid should contain 32 digits with 4 dashes" when trying to open the connection

Im developed a software and it's now time to test it connection to a online server.
Im developing in C# using Visual Studio and I'm trying to connect to a MySQL server. To be specifc Im trying to connect to a test server at db4free server.
I changed my ConnectionString to:
connectionString = "SERVER=db4free.net;PORT=3306;DATABASE=prpsystem;UID=database;PWD=password;";
But when I try to open the connection to check if the login is OK the visual studio shows this message error:
Guid should contain 32 digits with 4 dashes.
Do you guys know what is happening? Thanks for the help.
Your crash is coming from Driver.LoadCharacterSets, which is executing SHOW COLLATION. This is bug 92982 that has been reported against the Connector/NET driver.
According to the OP on that case, he fixed the problem by disabling the skip-character-set-client-handshake setting in his my.cnf for MySQL Server. If you also have that setting, try disabling it to see if it resolves the problem.
Otherwise, you could switch to MySqlConnector, an alternate ADO.NET library for MySQL, which doesn't have this bug.

C# XAMPP Connection

I have followed this tutorial - https://www.youtube.com/watch?v=2g4MPJ3fJt0 - Only inserting it as for anyone who is looking to connect to XAMPP with C#, this is a great tutorial.
However,
When I try and replicate the connection variable I seem to get an error.... (beginning to think it's not the best tutorial - joke)
connString = "Data Source =127.0.0.0.1;Port =3306;User ID=root;Password=;Database=aa";
(Local host)
I get a keyword not support 'port' error.
I can't find any information online that is related to Windows Forms, all seems to be for .NET development.
Can someone post the formatting for XAMPP connection for WPF (if it has been updated) or inform how to import Port into the SqlConncetion syntax
The tutorial is using XAMPP just to install the database for you, at the end of the day your problem is between MySQL and your C# code.
You are using a connection string with spaces
connString = "Data Source =127.0.0.0.1;Port =3306;User ID=root;Password=;Database=aa";
However the tutorial uses no spaces:
connString = "datasource=127.0.0.1;port=3306;username=root;password=;database=test;";
It could help if you check your connection to your mysql database, you can download Mysql workbench and try to connect from there, so you can be sure if your database is well configured or not

oracle connection string with SID in c#

Hi I have to connect to an Oracle Database( about which I know a little) using a windows application.
The windows application will not necessarily be in the same system.
I just needed the connection string.
So I Used Add connection functionality in Visual Studio 2014 to test the connection and get the string.
eedb is the SID which i read in stackoverflow question
Now using above, i was able to connect to the database using this functionality and even in my visual studio server explorer all the tables of the oracle database were showing but I needed to use the connection string in the windows application.
So I used following string:
DATA SOURCE=172.31.8.21:1521/eedb;USER ID=PDB_E_GND_R
I added password too to this string as
DATA SOURCE=172.31.8.21:1521/eedb;USER ID=PDB_E_GND_R;PASSWORD=123
when i run the application i get error.
System.Data.OracleClient.OracleException: ORA-01017: invalid username/password; logon denied.
So :
Why I am getting this error. Now some may mark this question as duplicate and even point out the answer can be found in issue stackoverflow question
Coz if this was the case I would have been unable to establish connection through add connection functionality of Visual Studio at all.
Please note: I added the reference: Oracle.DataAccess
And also for a programmer like me who has very little knowledge regarding the oracle.
How I can know which connection string I have to use for a particular oracle db.
Try the following connection string EZ connect does not seem to be so EZ
data source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 172.31.8.21)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = eedb)));USER ID=PDB_E_GND_R;PASSWORD=123

Authentication with old password no longer supported, use 4.1 style passwords

I have my website with hostgator and I want to access mysql database with C# windows application but when I tried to connect got this message:
"Authentication with old password no longer supported, use 4.1 style
password"
I have tried given solution:
SET SESSION old_passwords=0;
SET PASSWORD FOR user#host=PASSWORD('your pw here');
first query executed successfully but I got the error "Access denied for user#host" when second query executed. I can't understand why there is this problem. I am using MySQL-connecter-net 6.6.5.
I successfully connect my database with MySql workbench 5.2.47.
Can anyone help me what I can do more?
I have contact my hosting site and they make changes to my.cnf file to use 4.1 style password. and i am able to connect with mysql
mysql -u <username> -p <password> -h <hostname>;
but when i tried to connect with C# with mySQL connecter net 6.6.5 i again got this error.
I am using Visual Studio 2012 with MySQL connector 6.6.5 on Windows 8 64 bit. this is the code i used to connect
using MySQL.Data.MySQLClient;
private void button1_Click(object sender, EventArgs e)
{
string connStr = String.Format("server={0};port={1};uid={2};password={3};database={4}",
txtserver.Text, txtPort.Text, txtUser.Text, txtPassword.Text, txtDatabase.Text);
conn = new MySqlConnection(connStr);
try
{
conn.Open();
MessageBox.Show("Test Connection Succeded");
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
I can't understand where the problem is. Please Help me
I had the same problem. In my case just ran the command below connected on my database by the Workbench:
SET SESSION old_passwords=0;
SET PASSWORD FOR my_user=PASSWORD('my_password');
After that I could connnect using MySql Connector 6.6.5 in the c# code with the common mysql connection string:
"server=my_server_ip;user=my_user;database=my_db;port=3306;password=my_password;"
Today I had a similar problem in my C# application. I tried connecting to a server at hostgator.com (mysql) and connecting to localhost. After reading many posts and making many alterations, following every step but I couldn't connect to my hostgator mysql.
The solution in my case was to change the MySql Connector/NET library from version 6.* to version 5.*. After the change my application connected successfully.
While this might not be a viable solution for everyone, it may work for some.
I met this problem today, but I'v fixed it as below:
SET old_passwords=FALSE;
SET PASSWORD = PASSWORD('new pwd here');
Well, maybe the server's connection version is lower than the client, so you have to run this:"SET old_passwords=FALSE;"
Good Luck!
Had the same problem using MySqlClient package for C#. Let's break it down:
We need the database to not use old style passwords, thus we perform
SET old_passwords=0;
NOTICE there is no "SESSION" keyword. This is what allowed only your first query to work, "SESSION" implies temporary and any session settings are lost after a session ends.
Next we need to set the password of the user#host to the new password style
SET PASSWORD FOR user#host = PASSWORD('password');
This two should allow your MySqlClient
Just wanted to add to #diangelisj answer and say the root of the problem is the version of PhpMyAdmin installed on your server. In order to use the latest MySQL Data Connector class, you have to have the latest PhpMyAdmin (or issues arise). In my case, I have PhpMyAdmin 4.0.10.7 and tried using MySQL Data Connector 6.7.9, which gave the error.
What I did:
Download version 5.2.7 from https://dev.mysql.com/downloads/connector/net/6.9.html
Add .dll as reference to application
Presto!
Forget about applying SET old_password=0 since if the server is configured to use new password there is no reason to change the flag on the server if your app does not access anymore. Investigate the connector NET that you are using.
Connector 6.5.4 is the right one to use old_password, rencent version are using the new one.
The best practice would be to avoid to install the connector on the machine, just leave the NET to handle the dll if you already have it or anyway try to find MySQL.Data.DLL version 6.5.4.0 (384 kbytes)
I just solved my problem just like this:
1) Connect to the db with mysql workbench (select the "use old auth" and "send pass in cleartext"
2) Using the workbench, I ran the following several times (because it kept saying '0 rows affected'): (also change userID and password to your proper stuff)
SET SESSION old_passwords=0;
SET PASSWORD FOR userID=PASSWORD('password');
SET SESSION old_passwords=false;
SET PASSWORD FOR userID=PASSWORD('password');
3) Now go back to your app and it should run...
That is how I did it at least. I am using IX mysql and they do use old auth on their server so you must do something on your end...
I have not managed to solve the problem as the others who have responded, although I tried out everything that has been proposed.
In the end it turned out that the server on which stood my DB is not updated phpMyAdmin since version 3.5.5, and is currently being used everywhere version 4.1.14 and it is a problem.
The solution is to update your host phpMyAdmin or your own mySQL back to an earlier version in order to continue working on remote DB.
You may need this knowledge in order to solve some doubts that have arisen. :)
Today I found the same problem. I downloaded Workbench. There are some settings for workbench to connect database, as follows:
1.select tab SSL , select option 'no' in "Use SSL".
2. select tab advanced, select in Use the ole authentication Protocol.
after that Workbench working.
and follow tai1001' post.
That are all I found all steps that work.

Categories