简体   繁体   中英

Java MySQL Connect to server

I have a code which runs fine on the machine, where i have installed the database.

I am trying to run this code from other machine, which connects to the machine where MySQL is installed and tries to update data. However this code is not working from other machine. i am trying to connect as follows.

ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://192.168.0.104:3306/sd_mmm_data");
ds.setUsername("<UserName>");
ds.setPassword("<PassCode>");
ds.setMaxIdle(1);
ds.setMaxWaitMillis(40000);

I have replaced the localhost with the corresponding IP. However when i run the code, i always get the error table not found.

I have created a new user which can connect from other machines and the user has all of the required privileges. i have tried connecting from cmd prompt on client system and the connection works as expected queries run as expected. i tried connecting from mysql WorkBench and the connection works from client system. but the connection doesn't work from Java code. What am i missing?

I have added an entry in client system hosts file as follows ServerName And i tried using the ServerName in place of IP address in java code. Even this didn't work.

I think you have to use grant the remote user which enables access for remote user.

GRANT ALL PRIVILEGES ON . TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Created new set of users. Flushed privileges but it didn't work. Then went on with shutting down the database and tried restarting it. Even it didn't work. But tried fiddling with settings and tried creating new users and tried connecting, which finally worked. Don't exactly remember why it worked as i touched multiple network settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM