1

I'm trying to make a java program for a library management systems that uses a mySQL database. My current code allows for myself to access the database freely because it's the same computer hosting the database. I'm trying to make it so any computer on my same network running the application can access the database. I've done some research and found that I apparently need to comment out this bind-address line, but I can't find it in my my.ini file. I don't know if it will help because i'm fairly new to this but I'll leave here the current code im using to connect to the database in java. Overall im hoping to edit this code and configuration file to allow users on the same network to access this databse im hosting from my laptop.

package librarymanagement;

import java.sql.*;


public class ConnectionDB {
    final String dbURL = "jdbc:mysql://localhost:3306/library?useSSL=false";
    final String dbUser = "root";
    final String dbPassword = "testPass";
    
    public Connection connectDB(){
            Connection con = null;
        try {    
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(dbURL,dbUser,dbPassword);
            
        //Multi catch statement that handles SQL exceptions 
        //and ptints which line the error has occurred at
            
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        }
        return con;
    }

What should I do as that bind-address line is not in the my.ini file.

I've already through the my.ini file throughroughly yet still cant find the bind-address line.

Nicholas C
  • 15
  • 4
  • 1
    Basically the connection string will be similar. Just replace localhost with the IP address of your computer. BTW please don't insert screenshots. Just copy and paste the relevant part of code. It will be easier for us to read and analyse. – Mar-Z Mar 26 '23 at 17:04
  • @Mar-Z I've tried this before but I get the following error: java.sql.SQLException: null, message from server: "Host '*Hostname placeholder example*' is not allowed to connect to this MySQL server" – Nicholas C Mar 26 '23 at 17:21
  • possible duplicate of https://stackoverflow.com/questions/35942508/change-mysql-listening-bind-address-on-windows – martin Mar 26 '23 at 18:54

1 Answers1

0

I apparently need to comment out this bind-address line, but I can't find it in my my.ini file.

The instructions you are reading are taking for granted that you have prior knowledge that commenting out a line is the same as deleting that line, or if the line was never in the file. The goal is that the configuration directive is not active.

MySQL config files typically do not have every configuration directive (there are hundreds). So it's not surprising that the bind-address directive is not present in the current file.

If a directive is not in the file, or if it's commented out, it uses a built-in default value in the MySQL Server. The default value for bind_address is * according to the manual: https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_bind_address

Remember if you edit the config file, you must restart MySQL Server for the change to take effect, because it reads the config file only during startup.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • I appreciate the insight, so knowing that configuration directive isn't present how would I go about solving the error seen in my response to Mar-Z's solution? – Nicholas C Mar 26 '23 at 17:29
  • You must use GRANT to allow users to connect. See https://stackoverflow.com/q/19101243/20860 – Bill Karwin Mar 26 '23 at 17:35
  • I've tried this and there is no error on my local machine, I will test now on another laptop and come back with updates. I really do appreciate the help as this will really be a great detail in my IB computer science IA project. – Nicholas C Mar 26 '23 at 17:56
  • You can also test locally by using "127.0.0.1" as the hostname instead of "localhost" in your JDBC URL. This will force the client (i.e. your Java app) to connect via TCP/IP even though it is local. This requires that you have created a root user that is allowed it to connect via TCP/IP, something like `CREATE USER 'root'@'%' IDENTIFIED BY 'testPass';` – Bill Karwin Mar 26 '23 at 18:08
  • It may be unexpected but `'root'@'localhost'` and `'root'@'%'` and `'root'@'192.168.%'` (or other IP address) are literally different users to MySQL. They can even have different privileges and different passwords. – Bill Karwin Mar 26 '23 at 18:09
  • 1
    Everything worked tested on all the computers here at home, thanks for the advice. – Nicholas C Mar 26 '23 at 19:35