阿里云部署-2mysql

11次阅读

共计 1931 个字符,预计需要花费 5 分钟才能阅读完成。

mysql

下载

直接使用 yum 快速搭建

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 服务

启动

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

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

[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

停止

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

mysql 服务停止了

[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 密码,设置远程登录权限

先设置免密登录

vim /etc/my.cnf

在 mysqld 下面添加
skip-grant-tables
保存后重启 mysql,这时就可以跳过密码来登录 mysql 了

systemctl stop  mysqld.service
systemctl start  mysqld.service
mysql

先刷新

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 了。

测试

我用的是 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;

正文完
 0