如灰尘保佑的礼物

开发笔记(3)——mysql集成

环境搭建

数据库

安装mysql数据库

brew install mysql

启动mysql服务

brew services start mysql
brew services stop mysql    // 停止服务
brew services restart mysql // 重启服务
brew services list          // 服务列表

数据库配置

mysql -u root -p            // 使用root登录mysql
CREATE DATABASE dudu;       // 创建数据库
CREATE USER 'dudu'@'%' IDENTIFIED WITH mysql_native_password BY 'dudu';  // 创建用户
GRANT USAGE ON 'dudu'.* TO 'dudu'@'%';             
ALTER USER 'dudu'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON 'dudu'.* TO 'dudu'@'%';   // 授权dudu库全部权限
FLUSH PRIVILEGES;           // 刷新权限

安装mysql

npm install -save mysql @types/mysql

编写测试用例

import 'mocha';
import * as chai from 'chai'
import supertest from "supertest";
import * as mysql from 'mysql';

// 测试套件/组
describe('mysql测试', () => {
    // 测试用例
    it('连接测试', (done: Mocha.Done) => {
        var connection = mysql.createConnection({
            host     : '127.0.0.1',
            user     : 'dudu',
            password : 'dudu',
            database : 'dudu'
        });
        
        connection.connect(function(err) {
            expect(err).to.be.an('null');
            done();
        });
    })
})