【问题标题】:"Error: ER_NO_SUCH_TABLE: Table 'managementapp.medicament' doesn't exist"“错误:ER_NO_SUCH_TABLE:表 'managementapp.medicament' 不存在”
【发布时间】:2022-02-02 04:41:37
【问题描述】:

我正在使用 react、node 和 express 在简单的 CRUD 应用程序中工作。所以我连接到我的 mysql 数据库“managementapp”并尝试在我的数据库中插入信息,但我总是收到错误“表没有”当我在本地测试时它存在并且它在 mysql wrokbensh 中正常工作并且我确保我正确拼写我的表的名称。所以这是 App.js 的代码用于反应:

function App() {
  const [name,setName]=useState("");
  const [description,setDescription]=useState("");
  const [number,setNumbre]=useState(0);

  const addMedicament = () => {
    console.log(name);
    Axios.post("http://localhost:3001/create",
    {name:name,
    description:description,
    number:number,
  }).then(()=>{
    console.log("succes");
  });
  };

这是我的 index.js 用于后端部分

const db=mysql.createConnection({
    user:"root",
    host:"localhost",
    password:"",
    database:"managementapp",
    

});


app.post("/create", (req, res) => {
    console.log(req.body);
    const name=req.body.name;
    const description=req.body.description;
    const number=req.body.number;

    db.query(
    "insert into medicament (name,description,number) VALUES (?,?,?);",
    [name,description,number],
    (err,result) =>{
         if (err) 
         { console.log(err); 
        }
        else { res.send("Values inserted !! "); 
    }
    }
    );
});

所以我总是得到这个错误

{ name: 'erty', description: 'rtyuk', number: '86' }
Error: ER_NO_SUCH_TABLE: Table 'managementapp.medicament' doesn't exist
    at Query.Sequence._packetToError (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)   
    at Query.ErrorPacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
    at Protocol._parsePacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:88:28)
    at Socket.<anonymous> (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:526:10)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
    --------------------
    at Protocol._enqueue (C:\Users\Chahine\Management\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Connection.query (C:\Users\Chahine\Management\server\node_modules\mysql\lib\Connection.js:198:25)
    at C:\Users\Chahine\Management\server\index.js:25:8
    at Layer.handle [as handle_request] (C:\Users\Chahine\Management\server\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Chahine\Management\server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Chahine\Management\server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Chahine\Management\server\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:341:12)
    at next (C:\Users\Chahine\Management\server\node_modules\express\lib\router\index.js:275:10) 

{
  code: 'ER_NO_SUCH_TABLE',
  errno: 1146,
  sqlMessage: "Table 'managementapp.medicament' doesn't exist",
  sqlState: '42S02',
  index: 0,
  sql: "insert into medicament (name,description,number) VALUES ('erty','rtyuk','86');"
}

【问题讨论】:

  • 这通常表明您已连接到另一台服务器,该服务器没有桌面喷气机。
  • @nbk 如果我连接到另一台服务器,它将给出数据库不存在的错误“未知数据库”..但他说该表不存在..这意味着它找到了数据库,但没有找到里面的表
  • 服务器在这种情况下总是正确的,也许你有错字.. 对你的数据库做一个 mysqldump 并检查异常

标签: mysql node.js reactjs express


【解决方案1】:

我从昨晚开始一直在搜索,我发现问题出在我的 mysql 安装上.. 所以我卸载它并重新安装它然后我将端口从 3306 更改为另一个原因它已经在使用.. 然后我有了关于错误

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol 
   requested by server; consider upgrading MySQL client

所以我只是进入 mysql 并运行一个查询说 root 可以使用旧的 mysql_native_password 方法进行身份验证:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';

一切顺利。感谢Aidin的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-14
    • 2020-12-07
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多