zoukankan      html  css  js  c++  java
  • Node.JS + Mysql数据库

    服务嘛,当然离不开数据库了,你要是见到数据就哭了,我建议你还是看看本文,不要做数据哭啊,哈哈哈

    要做 ‘数据酷’嘛,哈哈哈

    一 安装

      1. wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

      2. yum -y install mysql57-community-release-el7-10.noarch.rpm

      3. yum -y install mysql-community-server

    二 启动

      systemctl start mysqld.service

    三 改密码

      1. 找到初始密码,grep "password" /var/log/mysqld.log

      

      第一个冒号后边的就是初始密码,当然,你的机器上会不同,可别抄我的啊

      2. mysql -uroot -p,之后要你输入密码,你就输入你查到的那个密码就登陆Mysql了

      3. 改变密码

        3.1 set global validate_password_policy=0;

        3.2 set global validate_password_length=1;

        3.3 ALTER USER 'root'@'localhost' IDENTIFIED BY 'your new password';

    四 删除临时仓库

      yum -y remove mysql57-community-release-el7-10.noarch

    五 DEMO

     

    var mysql = require('mysql');
    var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password:'11111111',
    database : 'test'
    });

    connection.connect();

    connection.query('select 1+1 as solution', function(error, results, fields) {
    if (error) throw error;
    console.log('The solution is: ', results[0].solution);
    });

    var addSql = 'INSERT INTO websites(Id,name,url, alexa,country) VALUES(0,?,?,?,?)';
    var addSqlParams = ['zcl','https://zcl.com', '11111111','CN'];
    connection.query(addSql,addSqlParams,function(err,result) {
    if (err) {
    console.log('[INSERT ERROR] -',err.message);
    return;
    }
    console.log('------------------------insert---------------------------');
    console.log('INSERT ID:', result);
    console.log('---------------------------------------------------------');
    });

    var modSql = 'UPDATE websites SET name=?,url=? WHERE Id=?';
    var modSqlParams =['woodzcl','http://zcl.com',6];
    connection.query(modSql, modSqlParams,function(err, result) {
    if (err) {
    console.log('[UPDATE ERROR] - ', err.message);
    }
    console.log('------------------------update---------------------------');
    console.log('UPDATE affectedRows:', result.affectedRows);
    console.log('---------------------------------------------------------');

    });

    var delSql = 'DELETE FROM websites WHERE Id=6';
    connection.query(delSql,function(err, result) {
    if (err) {
    console.log('[DELETE ERROR] - ',err.message);

    return;
    }
    console.log('------------------------delete---------------------------');
    console.log('DELETE affectedRows:', result.affectedRows);
    console.log('---------------------------------------------------------');
    });

    var sql = 'SELECT * FROM websites';
    connection.query(sql, function(err, result) {
    if (err){
    console.log('[SELECT ERROR]- ', err.message);
    }
    console.log('------------------------select---------------------------');
    console.log(result);
    console.log('---------------------------------------------------------');
    });

    //结果

    node test.js
    The solution is: 2
    ------------------------insert---------------------------
    INSERT ID: OkPacket {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 9,
    serverStatus: 2,
    warningCount: 0,
    message: '',
    protocol41: true,
    changedRows: 0 }
    ---------------------------------------------------------
    ------------------------update---------------------------
    UPDATE affectedRows: 0
    ---------------------------------------------------------
    ------------------------delete---------------------------
    DELETE affectedRows: 0
    ---------------------------------------------------------
    ------------------------select---------------------------
    [ RowDataPacket {
    id: 1,
    name: 'Google',
    url: 'https://www.google.cm/',
    alexa: 1,
    country: 'USA' },
    RowDataPacket {
    id: 2,
    name: 'weixin',
    url: 'https://www.taobao.com/',
    alexa: 13,
    country: 'CN' },
    RowDataPacket {
    id: 3,
    name: 'zcl',
    url: 'http://www.runoob.com/',
    alexa: 4689,
    country: 'CN' },
    RowDataPacket {
    id: 4,
    name: weibo',
    url: 'http://weibo.com/',
    alexa: 20,
    country: 'CN' },
    RowDataPacket {
    id: 5,
    name: 'Facebook',
    url: 'https://www.facebook.com/',
    alexa: 3,
    country: 'USA' },
    RowDataPacket {
    id: 7,
    name: 'zcl',
    url: 'https://zcl.com',
    alexa: 11111111,
    country: 'CN' },
    RowDataPacket {
    id: 8,
    name: 'zcl',
    url: 'https://zcl.com',
    alexa: 11111111,
    country: 'CN' },
    RowDataPacket {
    id: 9,
    name: 'zcl',
    url: 'https://zcl.com',
    alexa: 11111111,
    country: 'CN' } ]
    ---------------------------------------------------------

    Finally:

      好了,Node.JS 我打算就到这里了,如果你还有什么疑问,我还是愿意和你一起探讨的。

      再会!

  • 相关阅读:
    android sdk manager 更新问题
    Java static的使用 --Java笔记
    day98-django-ORM操作,创建表,表的增删改查,CBV操作
    day97-django-自带分页
    day96-django-分页模块(自定义)
    day95-django-子版继承母版
    day94-django-cookie登录校验
    day92-django-pymysql操作mysql封装之优化版,自定义的sqlhelper.py模块
    day91-django学生列表之增删改查(模态框-ajax提交)
    day90-django学生列表之增删改查(form提交)、思路、关于ajax的知识点
  • 原文地址:https://www.cnblogs.com/woodzcl/p/7543606.html
Copyright © 2011-2022 走看看