how can i connect to mysql database from my PC

Posted August 24, 2020 982 views

as i was following the tutorial to install mysql i have been struggling to connect from my MAC,

i already changed the bind-address in mysql conf to the public ip4 address.

i have enabled root and i created a separate user account, but still cant connect.

i also tried with sudo ufw allow 3306/tcp, sudo service ufw restart.

the error I’m getting is the following.

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. com.mysql.cj.exceptions.CJException: null, message from server: “Host ‘C-XX-XXX-XXX-XXX.{some host}.net’ is not allowed to connect to this MySQL server”.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Based on your error message above, I believe that your MySQL and UFW configuration is correct and your MAC has successfully connected to the MySQL server software. But MySQL refused your MAC connection because the MySQL account used to connect was not allowed. That means you entered a wrong host value when creating the separate user account. Try to create a different MySQL account with the correct host value C-XX-XXX-XXX-XXX.{some host}.net mentioned in the error message. For example:

CREATE USER 'sammy'@'C-XX-XXX-XXX-XXX.{some host}.net' IDENTIFIED BY 'password';

  • Hi Tom, Thanks for Replying. worked like a charm!

  • i spoke too, soon.

    can i ask you a follow up?

    now I’m able to connect to my DB trough apps or shell on a remote PC to the mysql server.

    but the app I’m trying to connect is still throwing an error.
    i created a blank new java app with jdbc.

    conn = DriverManager.getConnection(“jdbc:mysql://{PUBLICIP4ADRESS}/{DB_NAME}?user={user}&password={password}”);

    and when opening connection i get the following error.

    SQLException: Access denied for user ’{user}’@‘X-XX-XXX-XXX-XXX.{some host}.net’ (using password: YES)
    SQLState: 28000
    VendorError: 1045

    • Each MySQL account is associated with a user and the hostname (or IP) of a server as specified in the CREATE command. If you create an account associated with hostname A, but then your Java code connects to MySQL from server B, then that account will not work. You must create a different account with hostname B to connect from server B. In other words: you can only use your MySQL account on the computer specified in the CREATE command.

      So you can try creating another account with the user and hostname as mentioned in your SQLException message. (You can copy/paste to avoid typos)