Alibaba cloud deployment 2.mysql

Time:2019-10-20

Alibaba cloud deployment 2.mysql

mysql

download

Directly use Yum to quickly build.

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

Start stop MySQL service

start-up

[[email protected] ~]# systemctl start  mysqld.service

Use PS – EF | grep Mysql to view and find that the MySQL service has been started.

[[email protected] ~]# 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

Stop it

[[email protected] ~]# systemctl stop  mysqld.service

MySQL service stopped

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

Reset root password and set remote login permission

Set password free login first

vim /etc/my.cnf

Add under mysqld
skip-grant-tables
Restart MySQL after saving, and then you can skip the password to log in to MySQL.

systemctl stop  mysqld.service
systemctl start  mysqld.service
mysql

First refresh

mysql>flush privileges;

Create user
MySQL > create user ‘root’ @ ‘localhost’ identified by ‘your password’;
Allow root to log in remotely
mysql>grant all privileges on .To ‘root’ @ ‘%’ identified by ‘your password’;
Refresh
mysql>flush privileges;
Remove the password free setting in my.cnf and log in with a password

MySQL - U root - p 'your password'

You also need to add security group rules as before, and open port 3306 on the server firewall.
After configuration, you can connect mysql on the local remote server.

test

I use the sequenize package on NPM, which is based on promise and supports ES6 syntax. In addition to MySQL, it can also be used to connect to Postgres, MariaDB, SQLite, and Microsoft SQL server. (https://www.npmjs.com/package…

Combined with the development documents, we can carry out the actual development.

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;

Alibaba cloud deployment 2.mysql