Nodejs remote link to mysql database summary, nodejsmysql
Nodejs linked to the remote mysql, which took a long morning to complete. I thought it was okay to use it directly, but I couldn't find it. I finally found a method after reading various materials.
Nodejs links to a remote database in several steps:
1) install the node-mysql driver package
2) configure the mysql database
3) set system firewall
Once you install the node-mysql installation package, it may be relatively simple. You can directly install it using the npm Installation tool. npm install mysql -- save
2. Configure the mysql database
The mysql Server configures the username and password for accessing the database remotely. You can directly modify the user table in the mysql database to add your username and password, or grant the user name to access the data and set the access permission through grant authorization. The main statement is as follows:
Grant select, insert, update on database. Table to username @ 'hostname' identified by 'Password'
Flush privileges;
Note the following:
(1) flush privileges must be executed. After the mysql configuration is modified, it needs to be restarted. I use ubuntu12.04 server, so my restart command is/etc/init. d/mysql restart;
(2) Pay attention to the configuration of the mysql configuration file:/etc/mysql/my. cnf. If remote access is required, change bind-address: address to 0.0.0.0.
(3) Note that the host corresponding to the user name in the myqls. user table must be specified if it is only a specific address or domain name. If it is an arbitrary address, change it to %.
Three firewall settings
My database server access policy is to access mysql in remote linux through a web site (nodejs) in windows. Therefore, you need to ping the network first. If the network ping is normal, therefore, you need to use the telnet tool to access the remote end. For example, telnet ip port. If there is a problem with access, it indicates that the firewall configuration in linux is faulty and the traffic from port 3306 needs to be opened.
To configure the firewall, You need to modify or add iptables rules. On the unbuntu12.04 server, you can view the firewall configuration through iptables-L.
Then, run the iptables command to modify the firewall configuration. As my goal is to open port 3306, I used the following command to set the firewall.
Iptables-a input-p tcp-I eth0 -- dport 3306-j ACCEPT
After setting the firewall, you need to save iptables-save.
To keep the firewall valid at next startup, you need to save the rules to an independent file. The firewall is automatically configured as the system starts. If the firewall configuration rules are saved to the iptable. rules file, you must use
Iptable-save> iptables. rules
Then set the automatic configuration for startup, modify the/etc/network/interfaces file, and add the following under the corresponding NIC:
Pre-up iptables-restore </etc/iptables-rules. Save the settings.
This completes the firewall configuration.
It is relatively simple, but it has been a long time. record it.