Record a bug that occurs when myeclipes is connected to mysql in linux in the Window system.

Source: Internet
Author: User

Record a bug that occurs when myeclipes is connected to mysql in linux in the Window system.

Record a bug in mydomaines remote connection to mysql

Today, when I was playing with the hibernate framework, there was a very confusing bug. Let's not talk about it. Let's look at the bug first.

Access denied for user 'root'@'localhost' (using password:YES)

Then I searched for Baidu, some of which were about changing the password and some of which were about permission issues, I have to connect to mysql in linux under the window system (My mysql database is installed under the virtual machine)

Based on conditioned reflection, I checked my hibernate configuration file

<Hibernate-configuration> <! -- Remember: first configure the sessionFactory tag. A database corresponds to a sessionFactory tag --> <session-factory> <! -- Required parameter, four major parameters, database Dialect --> <property name = "hibernate. connection. driver_class "> com. mysql. jdbc. driver </property> <property name = "hibernate. connection. url "> jdbc: mysql: // 192.168.174.130: 3306/hibernate_day01 </property> <property name =" hibernate. connection. username "> root </property> <property name =" hibernate. connection. password "> root </property> <! -- Database dialect --> <property name = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </property> <! -- Optional configuration --> <! -- Ing configuration file, which must be mapped to the configuration file --> <mapping resource = "com/heima/domain/Customer. hbm. xml "/> </session-factory> 

If the configuration is still correct, it may be a permission issue.

1. First, grant permissions to the local user for the newly created database.

grant all privileges on hibernate_day01.* to root@192.168.174.130 identified by 'root';

2. Activate all Internet permissions for the account.

grant all privileges on hibernate_day01.* to 'root'@'%' identified by 'root';

Of course, some permissions can be specified for all, such as restrictions on adding, modifying, and querying. The IP address is a remote access and I set the IP address in linux.

3. Make the preceding permissions take effect

flush privileges;

4. Test the connection

@ Test public void testSave () {/*** 1. load the configuration file first * 2. create the sessionFactory object and generate the Session object (Session) * 3. start transaction * 5. write the saved Code * 6. commit a transaction * 7. release resources * // 1. first load the Configuration file Configuration config = new Configuration (); // The hibenate under the src directory is loaded by default. cfg. xml configuration file config. configure (); // create the sessionFactory object SessionFactory factory = config. buildSessionFactory (); // creates the session object Session session = factory. openSession (); // enable Transaction tr = session. beginTransaction (); // write the stored code Customer c = new Customer (); // c. setCust_id (cust_id); the primary key is auto-incrementing and you do not need to set c. setCust_name ("test"); c. setCust_level ("2"); c. setCust_phone ("110"); // Save the data. The operation object is equivalent to the table structure session of the operation database. save (c); // submit the transaction tr. commit (); // release the resource session. close (); factory. close ();}

In this case, JUnit is displayed in green, indicating that the operation is successful.

 

Let's look at the database. The data has been inserted into the table.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.