阿里云部署 2.mysql

mysql

下载

直接使用yum快速搭建mysql

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server

启停mysql服务

启动es6

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# systemctl start  mysqld.service

使用ps -ef | grep mysql查看,发现mysql服务已经启动了。sql

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# ps -ef | grep mysql
mysql    21709     1  6 10:58 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     21738 21649  0 10:58 pts/0    00:00:00 grep --color=auto mysql

中止npm

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# systemctl stop  mysqld.service

mysql服务中止了vim

[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# ps -ef | grep mysql
root     21747 21649  0 10:58 pts/0    00:00:00 grep --color=auto mysql
[root@iZ8vbfhrv1vsbp44n9fdtoZ ~]# systemctl start  mysqld.service

重设root密码,设置远程登陆权限

先设置免密登陆promise

vim /etc/my.cnf

在mysqld下面添加
skip-grant-tables
保存后重启mysql,这时就能够跳过密码来登陆mysql了安全

systemctl stop  mysqld.service
systemctl start  mysqld.service
mysql

先刷新bash

mysql>flush privileges;

建立用户
mysql>create user 'root'@'localhost' identified by '你的密码';
容许root用户远程登陆
mysql>grant all privileges on . to 'root'@'%' identified by '你的密码';
刷新
mysql>flush privileges;
去掉my.cnf里的免密设置,使用密码登陆服务器

mysql -u root -p '你的密码'

你还须要按照以前的方法添加安全组规则,打开服务器防火墙上的3306端口。
配置完毕后你就能够在本地远程链接服务器上的mysql了。app

测试

我用的是npm上的Sequelize包,它是基于promise的,支持es6语法。除了mysql,它还能够用于链接Postgres、MariaDB、SQLite和Microsoft SQL Server。(https://www.npmjs.com/package...

结合开发文档,咱们就能够进行实际开发了。

const Sequelize = require('sequelize');
const config = require('../config');

const logger = require('log4js').getLogger('app');

class MysqlClient {
  constructor() {
    if (!MysqlClient.instance) {
      this.client = new Sequelize(config.mysql.database, config.mysql.username, config.mysql.password, {
        host: config.mysql.host,
        port: config.mysql.port,
        dialect: 'mysql',
        pool: {
          max: 5,
          min: 0,
          idle: 10000,
        },
        timezone: '+08:00',
      });

      const client = this.client;
      MysqlClient.instance = client;

      client
      .authenticate()
      .then(() => {
        logger.info('Connection has been established successfully.');
      })
      .catch(err => {
        logger.info('Unable to connect to the database:', err);
      });
    }
  }
}

module.exports = new MysqlClient().client;

相关文章
相关标签/搜索