为了账号安全,请及时绑定邮箱和手机立即绑定

阿里云部署 2.mysql

更多文章欢迎大家关注微信公众号 前端阅读室

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/sequelize)

结合开发文档,我们就可以进行实际开发了。

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;
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消