直接使用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
複製代碼
啓動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
複製代碼
先設置免密登陸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。(www.npmjs.com/package/seq…)
結合開發文檔,咱們就能夠進行實際開發了。
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;
複製代碼