Remotely connect to MySQL using an INI File

Source: Internet
Author: User

Recently, I wrote a new MySQL connection. To facilitate flexible application, read MySQL connection parameters by reading the INI file.

By the way, I learned about the INI file.

INI format.

The INI file consists of multiple sections.

[Network] // node host = *. * // host: Key, IP: Value: User = rootpwd = // It is null if no data is written.

Read:

GetPrivateProfileString("NETWORK","host","",host.GetBuffer(MAX_PATH),MAX_PATH,strFileName);

The following parameters are listed in sequence: node name, key name, lpdefault, key value, key value length, and ini file path.
Lpdefault: If the INI file does not contain the field name or key name specified by the first two parameters, this value is assigned to the variable.

Return Value: key value.

Write:

WritePrivateProfileString("NETWORK","host","1.2.3.4",strFileName);

The following parameters are listed in sequence: node name, key name, key value, and ini file path.

Returned value: long. A value of non-zero indicates success, and a value of zero indicates failure.

File Path:

1. The path of the INI file must be complete, and directories at all levels before the file name must exist. Otherwise, the write operation fails and the function returns false.

2. The file name path must be \, because in VC ++, \ indicates \.

3. You can also put the INI file in the directory where the program is located. In this case, the lpfilename parameter is: ". \ student. ini ".

After the configuration file is written, connect:

mysql_real_connect(&mydata,host,user,pwd,database,port,unix_socket,clientflag);

// The parameters are obtained by reading the file.

The data is connected to a bunch of posts at night and will not be repeated. The MySQL connection base class has been written. Upload to the resource later.

Another important step is to connect remotely:

You can remotely set the server:

1. Run
Mysql-u root-P MySQL, the system prompts to enter the password of the Database Root User, enter the MySQL console after the input is complete, the first MySQL of this command is to execute the command, the second MySQL is the system data name, which is different.
2. On the MySQL console, run grant all privileges on *. * To 'root' @ '%' identified by 'mypassword' with grant option;

3. mysql>
Flush privileges; // make the modification take effect immediately

A 10060 connection failure error may occur. Check whether the firewall has disabled port 3306.

Related Article

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.