-2

Quick question - is it possible to change the password of a user that has been entered into the "user" table under the MySQL database via a WinForm app?

I've been inserting and selecting data from a database I created, but if I ever wanted to change the MySQL password, I was wondering if this is possible via C#. Reason I say this is because ATM the connectionstring is hard-coded to take the username and password (maybe someone can give advice on how not to store the password in the connection string in App.config too).

Thanks :)

4
  • You can access the sys database. it stores the users and their passwords. Commented Jul 23, 2018 at 21:39
  • i would not store or hardcore a MySQL username/password with in a C# application at all. Especially if the C# application is meant to be downloadable and usable for the public.. Because a (C#) disassembler would make it possible to find the user name and password to a MySQL server... If the application is meant for internal use like a trusted network off people i think you will be ok. Commented Jul 23, 2018 at 21:45
  • @HagaiWild can't see where the users and passwords are stored in that database. Plus why are people downvoting this question? Commented Jul 23, 2018 at 21:46
  • @RaymondNijland thanks Raymond. Any suggestions on where else the connection string can be stored in order to connect to the MySQL database? Also, in regards to changing the password for a user stored in the "user" table, is it possible to modify the password through the app? I think the reasoning behind this was so that you don't have to rebuild the app everytime someone changes the MySQL password. Commented Jul 23, 2018 at 21:48

1 Answer 1

0

Take something like this post and change the statement in the command string to use an ALTER USER instead:

using(MySqlConnection Connection = new MySqlConnection("SERVER=localhost;UID=root;"))
using(MySqlCommand Command = new MySqlCommand("ALTER USER 'username'@'localhost' IDENTIFIED BY 'password';", Connection))
{
    Connection.Open();
    Command.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Andrew. ATM my connection string points to another database hosted by my server <connectionStrings> <add name="booking_system" connectionString="Server=rds-booking-system.cmtggasdasln.us-east-1.rds.amazonaws.com;Database=booking_system;Uid=saysawfdin;Pwd=XXXXX" providerName="MySql.Data.MySqlClient" /> </connectionStrings> So will I have to add another connection string but instead have the database as "mysql"?
Not sure I understand your issue. My answer will help you change the MySQL Users password. If you are changing the password of the user that is specified/hard coded in the Connection string then you will need to look at something like in this post to update the password stackoverflow.com/questions/360024/…
Perfect - thanks man. Also, if your MySQL database is older than 5.7.5 (which mine is), the above does not work. However, doing something like SET PASSWORD FOR 'user-name-here'@'hostname' = PASSWORD('new-password'); does :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.