Ali cloud server database connection

Connection procedure:

 first step:

    Certainly depends on your mysql database is started, in order to determine whether or not able to connect, there are two ways to look at

1.service mysqld status to view the status of your mysql

 

 

 

2.ps -e | grep mysqld check your mysql running the corresponding process

 

 

 

Step two: Turn on remote access to the mysql

The default is mysql user does not have permission to access remotely, so when a program with a database when not on the same server, we need to open the remote access mysql.

There are two main methods, changing table method and authorization method.

Relatively speaking, it is easier to change the table method, the individual is more inclined to use this method, therefore, this method only change the table posted

1, landing mysql

mysql -u root -p

2, modify mysql user table of the host key, to the localhost%. % Represented here it is to allow any host access, allow only certain ip access, you can change the corresponding ip, for example, can be changed to 192.168.1.123 localhost, which means that only allows the ip 192.168.1.123 LAN remote access mysql .

mysql> use mysql;

mysql> update user set host = '%' where user = 'root';

mysql> select host, user from user;

mysql> flush privileges;

 

 

The third step: the establishment of a new port in 3306 Ali cloud firewall, select the type of mysql

 

Step 4: Enable the port

1, open the firewall configuration file

vi  /etc/sysconfig/iptables

2, add the following line

 

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

 

3, reboot the firewall

 

Save Settings: service iptables save

service  iptables restart

 

Note compile time followed by the -lmysqlclient

 

Step Five: test

What is the test demo:

#include<stdio.h>
#include<stdlib.h>
#include<mysql/mysql.h>
#include<string.h>



int main(int argv,char *argc[])
{
    MYSQL mysql;
     
    mysql_init(&mysql);

    printf("test!!\n");
    if(!mysql_real_connect(&mysql,"**.**.**.**","root","pwd","mysql",3306,NULL,0 )) // Here is Ali cloud public URL and MySQL account password 
    { 
        printf ( " mysql_connect Fail \ the n-! " );
         Return  0 ; 
    } the else 
    { 
        printf ( " connectd MYSQL successs \ the n-! " ); 
    } 

    Printf ( " Test !! \ n- " ); 
    
        mysql_close ( & MySQL);
         return  0 ; 
    }

 

Guess you like

Origin www.cnblogs.com/yangjiquan/p/11368300.html