const mysql = require("mysql")
const pool = mysql.createPool({
host: "192.168.2.32",
port:"3306",
user: "root",
password: "root",
database:"wifi_module"
})
pool.query("select 1", (err, result) => {
if (err) return console.log(err.message);
console.log(result);
})
module.exports = {
pool
}
原因为主机(IP)XXX-XXX没有访问远程数据库的权限
1.登录数据库
mysql -u root -p
2.输入密码
mysql> use mysql;
3.查询host
mysql> select user,host from user;
4.创建host
如果没有"%"这个host值,就执行下面这两句:
mysql> update user set host='%' where user='root';
mysql> flush privileges;
5.授权用户
GRANT ALL ON *.* TO '你的密码'@'%';
(如果成功可不运行以下代码)
alter user 'root'@'%' identified with mysql_native_password by '你的密码'
(注意配置项参数名)
运行代码出现
[ RowDataPacket { '1': 1 } ]表示连接成功