nodejs入门之连接mysql

栏目: Node.js · 发布时间: 5年前

内容简介:最近工作项目有调整,需要熟悉一些前端的内容,就顺便看看node,同时边学习边总结一下。这篇文章主要总结node链接mysql相关的各种问题。

最近工作项目有调整,需要熟悉一些前端的内容,就顺便看看node,同时边学习边总结一下。这篇文章主要总结node链接 mysql 相关的各种问题。

示例Demo

  • 准备环境:

    ➜  1  mkdir node-mysql
      ➜  1  cd node-mysql
      ➜  node-mysql  tnpm install mysql
      npm WARN saveError ENOENT: no such file or directory, open '/Volumes/Document/Document/temp/1/node-mysql/package.json'
      npm WARN enoent ENOENT: no such file or directory, open '/Volumes/Document/Document/temp/1/node-mysql/package.json'
      npm WARN node-mysql No description
      npm WARN node-mysql No repository field.
      npm WARN node-mysql No README data
      npm WARN node-mysql No license field.
    	
      + mysql@2.16.0
      added 11 packages from 15 contributors and audited 13 packages in 1.524s
      found 0 vulnerabilities
    	
      ➜  node-mysql  touch demo.js
  • 准备事例代码,在 demo.js 中增加下面的代码

    var mysql = require('mysql');
    
      var con = mysql.createConnection({
        host: "127.0.0.1",
        user: "zixie",
        password: "zixie"
      });
    	
      con.connect(function(err) {
          if (err) {
              throw err;
          }else{
              console.log("Connected to zixie!");
          }
      });
    	
      con.query('SELECT 1 + 1 AS solution', function (err, results, fields) {
        	if (err) {
              throw err;
          }else{
              console.log('The solution is: ', results[0].solution);
          }
      });
  • 运行测试效果:

    ➜  node-mysql  node demo.js
      Connected to zixie!
      The solution is:  2

异常处理

    1. 连接mysql失败,报错: ER_NOT_SUPPORTED_AUTH_MODE 或添加权限出错
    • 可能错误信息1

      throw err; // Rethrow non-MySQL errors
        ^
      
        Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    • 可能错误信息2

      mysql> ALTER USER 'zixie'@'localhost' IDENTIFIED WITH mysql_native_password BY 'zixie';
        ERROR 1396 (HY000): Operation ALTER USER failed for 'zixie'@'localhost'
    • 解决方案

      root登录登录mysql,并查看用户信息

      ➜  1  mysql -uroot -p
        Enter password:
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 39
        Server version: 8.0.13 Homebrew
      		
        Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
      		
        Oracle is a registered trademark of Oracle Corporation and/or its
        affiliates. Other names may be trademarks of their respective
        owners.
      		
        Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      		
        mysql> ALTER USER 'zixie'@'localhost' IDENTIFIED WITH mysql_native_password BY 'zixie';
        ERROR 1396 (HY000): Operation ALTER USER failed for 'zixie'@'localhost'
        mysql> use mysql
        Reading table information for completion of table and column names
        You can turn off this feature to get a quicker startup with -A
      		
        Database changed
        mysql> select user,host from user;
        +------------------+-----------+
        | user             | host      |
        +------------------+-----------+
        | zixie            | %         |
        | mysql.infoschema | localhost |
        | mysql.session    | localhost |
        | mysql.sys        | localhost |
        | root             | localhost |
        +------------------+-----------+
        5 rows in set (0.00 sec)

      找到自己对应的账号,然后执行下面的授权命令,切记账号和host要对应:

      ALTER USER 'USERNAME'@'HOST' IDENTIFIED WITH mysql_native_password BY 'PASSWORD';

      例如

      mysql> ALTER USER 'zixie'@'%' IDENTIFIED WITH mysql_native_password BY 'zixie';
        Query OK, 0 rows affected (0.10 sec)
      		
        mysql>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)

程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)

左程云 / 电子工业出版社 / 109.00元

《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》是一本程序员代码面试"神书”!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一痛点,本书选取将近300道真实出现过的经典代码面试题,帮助广大程序员的面试准备做到接近万无一失。"刷”完本书后,你就是"题王”!《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》......一起来看看 《程序员代码面试指南:IT名企算法与数据结构题目最优解(第2版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码