For the MySQL cluster configured in the preceding dual-machine Redundancy backup and load balancing policy (MySQL cluster getting started installation and Configuration Guide), we should create a database below, using MySQL cluster for redundant backup and load balancing,
Access the database on any machine of 192.168.56.10 or 192.168.56.20. The command is:
1Bin/MySQL
For example, if I create the database assetscenterdb on 192.168.56.10, run the following command:
1 Create DatabaseAssetscenterdb
After successful creation, we can see the database we created on the machine 192.168.56.20. Next we will create a database table,
Pay attention toEngine is NDBFor example, a table structure in the database looks like this:
1 Create Table 'Ac _ asset '(
2 'Id' Int ( 11 ) Not Null Auto_increment,
3 'Type _ id' Int ( 11 ) Not Null ,
4 'Status' Tinyint ( 4 ) Not Null ,
5 'Sn' Varchar ( 50 ) Default Null ,
6 'Description' Text ,
7 'Eltms' Varchar ( 50 ) Default Null ,
8 'Order _ name' Varchar ( 50 ) Default Null ,
9 'Owner _ name' Varchar ( 50 ) Default Null ,
10 'Warehouse _ time' Varchar (50 ) Default Null ,
11 'Location' Varchar ( 100 ) Default Null ,
12 'Price' Double Default Null ,
13 'User_name ' Varchar ( 50 ) Default Null ,
14 Primary Key ('Id ')
15 ) Engine = NDB auto_increment = 1726 Default Charset= Utf8;
Only in this way can the two machines share the data structure of the database. At this time, run the following command on the other machine:
1Show tables
You can see the table we created.
So JavaProgramHow to access it, I use the JDBC driver for the mysql-connector-5.1.12, the connection string is as follows:
1JDBC: mysql: loadbalance://192.168.56.10:3306,192.168.56.20:3306/Assetscenterdb? Roundrobinloadbalance=True
Use roundrobinAlgorithmIf one of the servers is down, the inaccessible database servers will be automatically kicked off. If the server is restored, the load will continue to be split.
Other database operations are the same as normal operations.
Next, we also need to configure the database to allow remote access. For example, if I put the server on machine 192.168.56.30, I need to configure the two other machines separately:
1 Grant All On *.* ToRoot @"192.168.56.30"IdentifiedBy"Your password"
Then you can run the server and access it normally.
If your program cannot be normally Accessed at this time, you need to read the MySQL database "error 1042 (hy000): Can't Get hostname for your address" question in this blog post,
Because I encountered this problem, I can use the above configuration locally, but I put the machine in the lab, and MySQL cannot be connected, and error 1042 (hy000) occurs ): can't get hostname for your address error,
This problem may occur only when the DNS resolution of MySQL is different from that of lab in my local environment.