当我以 root 身份在我的服务器上安装 mysql_server 时开始收到此错误,我正在使用 ufw,我不知道是否应该通过我的 ufw 允许 mysql 服务器,如果应该的话,我不知道该怎么做,同样在我的本地服务器上我收到此错误:
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'mysql'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/mnt/c/Users/user/Documents/webdevelopment/projects/FinalYearProject/Black-Anthem-Ltd-Website/app.js:4:13)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
具体来说,它说它无法在我的目录中找到 mysql。在安装 mysql_server 之前,我试图将我的 html 表单连接到我的 mysql 数据库,然后开始遇到这个问题。这是我的 html 表单代码:主页订阅者表单:
<div class="subsect">
<div id="subtxt">
<h1>Subscribe to our Newsletter!</h1>
</div>
<div id="subform">
<form action="/" method="post">
<div class="ol"><div class="nam" >Name</div><div id="rsik">*</div></div>
<div class="sd ssd"><input type="text" name="fname" placeholder="First" id="fn">
<input type="text" name="lname" placeholder="Last" id="ln" ></div>
<div class="ol"><div class="nam" >E-mail</div><div id="rsik">*</div></div>
<div class="sd"></span><input type="email" name="email" placeholder="[email protected]" id="email"></div></br>
<button type="submit">Subscribe</button>
</form>
</div>
</div>
请求报价页面的表格:
<div id="formsection">
<form action="/RAQ" method="post" id="raqform">
<h2>Request A Quote</h2>
<div class="ol"><div class="nam" >Name</div><div id="rsik">*</div></div>
<div class="sd ssd"><input type="text" name="sfname" placeholder="First" id="fn">
<input type="text" name="slname" placeholder="Last" id="ln" ></div>
<div class="ol"><div class="nam" >E-mail</div><div id="rsik">*</div></div>
<div class="sd"><input type="email" name="semail" placeholder="[email protected]" id="email"></div>
<div class="ol"><div class="nam" >Select A Service</div><div id="rsik">*</div></div>
<div class="sd">
<select name="services" id="ser" class="sd">
<option value="RDCCC">Roads, Drainage, Culverts and channels construction</option>
<option value="CON">Consultancy</option>
<option value="DIC">Dams and irrigation Construction</option>
<option value="WMPFC">Warehousing, Marine and port facilities construction</option>
<option value="CHEIUPW">Construction of concrete hard surfaces especially in unfriendly polluted water</option>
<option value="CBHF">Consultation on buildings and feasibility</option>
<option value="CIMS">Calibration, inspection and meter proving services</option>
<option value="DCOGPPF">Design and Construction of oil and gas production and processing facilities</option>
<option value="DCPS">Design and Construction of pipeline Systems</option>
<option value="DCRPP">Design and Construction of refineries and petrochemical plants</option>
<option value="ROGF">Rehabilitation of oil and gas facilities (Onshore and Offshore)</option>
<option value="DCMTF">Design, Construction and Maintenance of tank farms</option>
<option value="DCMUS">Design, Construction and Maintenance of flow stations and underground storage</option>
<option value="LIP">Local and international procurement</option>
<option value="SSFP">Support services for projects</option>
<option value="CMSL">Civil and mechanical equipment supply and leasing</option>
<option value="HSEFPSMS">Health and safety environment and fire protection systems materials supply</option>
<option value="EFPSMS">Environment and fire protection systems materials supply</option>
<option value="SSS">Structural steel supply</option>
<option value="POCTG">Pipeline and oil country tubular goods (OCTG) supply </option>
<option value="WHCPS">Wellheads and control supply</option>
<option value="VFS">Valves and fittings supply</option>
<option value="PCFSS">Pumps, Compressor and fluid systems supply</option>
<option value="SSMPS">Strategic sourcing and man power supplies</option>
<option value="LSM">Logistics and supply chain management</option>
<option value="PM">Project Management</option>
<option value="FM">Facilities Management</option>
</select>
<div class="ol"><div class="nam" >Message</div><div id="rsik">*</div></div>
<div class="sd"><textarea name="comment" id="" cols="30" rows="10">Type your message here</textarea></div>
</div>
<button type="submit">Request A Quote</button>
</form>
</div>
这是我的 app.js 文件:
const express = require('express')
const path = require('path')
const app = express()
var mysql = require('mysql');
var bodyParser = require('body-parser')
const port = 3000
var urlencodedParser = bodyParser.urlencoded({ extended: false })
//Static Files
app.use(express.static(path.resolve(__dirname, 'public')))
//Set views
app.set('view engine', 'ejs')
app.set('views', './views')
app.get('/', (req, res) => {
res.render('Home')
})
app.get('/About', (req, res) => {
res.render('About')
})
app.get('/Projects', (req, res) => {
res.render('Projects')
})
app.get('/RAQ', (req, res) => {
res.render('RAQ')
})
app.get('/', (req, res) => {
res.render('home', {qs: req.query});
})
app.post('/', urlencodedParser, (req, res) => {
res.render('home', {qs: req.query});
})
app.get('/raq', (req, res) => {
res.render('raq', {qs: req.query});
})
app.post('/raq', urlencodedParser, (req, res) => {
res.render('raq', {qs: req.query});
})
var connection = mysql.createConnection({
host : 'https://www.blackanthemltd.site',
user : 'abayomi',
password : '',
database : 'subscribers'
});
connection.connect(function(err){
if (err) throw err;
console.log('connected..')
});
app.post('/', function(req,res){
console.log(req.body);
var sql ="insert into users values('"+req.body.fname+"','"+req.body.lname+"',"+req.body.email+")"
connection.query(sql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
res.render('Home', { title: 'Data Saved',
message: 'Data Saved successfully.'})
connection.end();
})
var connection2 = mysql.createConnection({
host : 'https://www.blackanthemltd.site',
user : 'abayomi',
password : '',
database : 'Quotation_requests'
});
connection2.connect(function(err){
if (err) throw err;
console.log('connected..')
});
app.post('/RAQ', function(req,res){
console.log(req.body);
var sql ="insert into users values('"+req.body.sfname+"', '"+req.body.slname+"', '"+req.body.semail+"', '"+req.body.services+"', "+req.body.comment+")"
connection2.query(sql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
res.render('RAQ', { title: 'Data Saved',
message: 'Data Saved successfully.'})
connection2.end();
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
这是我的远程服务器上的 package.json 文件:
{
"name": "balfyp",
"version": "1.0.0",
"description": "Final year project",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "abayomi",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"ejs": "^3.1.6",
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^2.0.12"
}
}
这是我实时服务器上的 package.json 文件:
{
"name": "balfyp",
"version": "1.0.0",
"description": "Final year project",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "abayomi",
"license": "MIT",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"ejs": "^3.1.6",
"express": "^4.17.1",
"mysql": "^2.18.1",
"nodemailer": "^6.6.3",
"nodemailer-mailgun-transport": "^2.1.3"
},
"devDependencies": {
"nodemon": "^2.0.12"
}
}
请问我做错了什么?我对代码库做了一些更改,我的网页不再显示错误并加载正常,这表示 mysql 连接成功,但在填写表单后单击表单上的提交按钮时出现错误:
Cannot POST /
以下是我对代码库所做的更改,特别是针对我的 app.js 文件:
const express = require('express')
const path = require('path')
const app = express()
var mysql = require('mysql');
var bodyParser = require('body-parser')
const port = 3000
var urlencodedParser = bodyParser.urlencoded({ extended: false })
//Static Files
app.use(express.static(path.resolve(__dirname, 'public')))
//Set views
app.set('view engine', 'ejs')
app.set('views', './views')
app.get('/', (req, res) => {
res.render('Home')
})
app.get('/About', (req, res) => {
res.render('About')
})
app.get('/Projects', (req, res) => {
res.render('Projects')
})
app.get('/RAQ', (req, res) => {
res.render('RAQ')
})
app.get('/', (req, res) => {
res.render('home', {qs: req.query});
})
app.post('/', urlencodedParser, (req, res) => {
res.render('home', {qs: req.query});
})
var connection = mysql.createConnection({
host : 'https://www.blackanthemltd.site',
user : 'root',
password : 'Mynameisabayomi1.',
database : 'bal'
});
connection.connect(function(err){
if (err) throw err;
console.log('connected..')
});
app.post('/', function(req,res){
console.log(req.body);
var sql ="insert into users values('"+req.body.fn+"','"+req.body.ln+"',"+req.body.email+")"
connection.query(sql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
res.render('Home', { title: 'Data Saved',
message: 'Data Saved successfully.'})
connection.end();
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
请问我怎样才能避免出现此错误。
答案1
如果 MySQL 与 Web 服务器在同一台机器上运行,则无需使用 UFW 进行任何修改。相反,您需要更改连接设置:
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'myWebApp',
password : 'superSecretPassword!123.',
database : 'bal'
});
笔记:
- 该
host
值通常为127.0.0.1
或localhost
当数据库与 Web 服务器位于同一系统时。 - 您不能再(轻松地)将
root
MySQL 帐户用于 Web 应用程序,因此您需要创建一个可以访问您正在使用的数据库的新帐户:- 连接到服务器上的 MySQL
笔记:这就是sudo mysql -u root
root
现在访问 MySQL 帐户的方式。您需要拥有sudo
服务器权限。这是一项安全功能,也是 Web 服务器无法root
再使用该帐户的众多原因之一。 - 创建一个新帐户并授予其对网站所需数据库的完全访问权限:
CREATE USER 'baluser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'superSecretPassword!123'; GRANT ALL ON `bal`.* TO 'baluser'@'localhost';
- 连接到服务器上的 MySQL
这应该可以给你提供开始所需的一切。