background

Continue with the previous articleFront-end to full stack starts hereComplete a preliminary understanding of the cloud server, as wellnginxThe next step isMySQLInstall, build tables, and use the Node.js frameworkKoaDo simple dataAdd and deleteFunction. It’s a long process, but follow these steps to make it easier.


MySQL installation

Log in to the cloud server, access the Linux console, and run the following command to install the cloud server

/ / 1. Download and install wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm RPM - the ivh mysql-community-release-el7-5.noarch.rpm yum update yum install mysql-server //2. Chown mysql:mysql -r /var/lib/mysql //3 Mysqld --initialize //4. Start systemctl start mysqld //5. Check the running status of MySQL systemctl status mysqldCopy the code


This section describes how to install Mysql

After Mysql is installed successfully, the default password of user root is empty. You can use the following command to create a password for user root

[root@host]# mysqladmin -u root password "new_password"; // Connect to Mysql server [root@host]# Mysql -u root -p Enter password: // Log in to Mysql -u root -p // Enter the password to exit or quitCopy the code

Possible problems

Mysql -u 175.23.23.23 -p [root@VM-0-5-centos bin]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)Copy the code

Solution: Find the my.conf file and edit it using vim my.conf (/etc/my.cnf). Add skip-grant-tables on any line, change the password, and run the service mysqld restart command to restart mySQL. Remember to delete skip-grant-tables after success.


Building a database

Now you need to add, delete, change, and review a list. Suppose you have a blog system that operates on the list of blogs. The table structure of the database is as follows:

id title content img author likeNum createdAt updatedAt
1 The article title content cover The author Thumb up for Creation time Update time
// Go to the cloud service console and log in to your MYSQL server. CREATE DATABASE test (all command lines end with a semicolon) CREATE DATABASE test; //2. Create a blog TABLE DROP TABLE IF EXISTS 'blog'; CREATE TABLE `blog` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `content` text, `img` varchar(255) DEFAULT NULL, `author` varchar(255) DEFAULT NULL, `likeNum` varchar(255) DEFAULT '0', `createdAt` datetime DEFAULT NULL, `updatedAt` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;Copy the code

You can run the following command to view the table

So we’re done here with the database

These are all command-line databases, but there are also many graphical interface tools available, such as Navicat Premium, which you can search yourself


Write server-side applications using KOA

Koa is a new Web framework, built by the same people behind Express, that aims to be a smaller, more expressive, and more robust cornerstone of web application and API development. By making use of async functions, Koa helps you discard callback functions and greatly enhances error handling.

  1. Initialize thekoaproject
Const koa = require('koa') const app = new koa () app.use(async (CTX) => {ctx.body = 'Hello World'}) app.listen(3000) // Run nodeapp.jsCopy the code

  1. Add, delete, change and check API
    • Installing a plug-in
    npm i koa-bodyparser sequelize koa-router moment mysql2 --save
    Copy the code
    • Database configuration (New utils foldersequelize.js)
    Const Sequelize = require(' Sequelize ') Password const sequelize = new sequelize ('test', 'root', 'password', {host: '111.111.111.111', // database IP address: 'mysql', operatorsAliases: false, pool: { max: 5, min: 0, acquire: 30000, idle: 10000}}) sequelize.authenticate (). Then (() => {console.log('MYSQL connection successful...... ')}). Catch (err => {console.error(' link failed :', err)}) sequelize.sync() module.exports = sequelizeCopy the code
    • router
    //router const nginxApi = '/api' const router = require('koa-router')() //blog const Blog = require('.. /controller/blog') router.get(`${nginxApi}/blog/list`, Blog.list) router.post(`${nginxApi}/blog/create`, Blog.create) router.post(`${nginxApi}/blog/destroy`, Blog.destroy) router.get(`${nginxApi}/blog/details`, Blog.details) router.post(`${nginxApi}/blog/update`, Blog.update) //404 router.get('/404', async (ctx, Next) => {ctx.body = 'Page Not Found' context.status = 404 await next()}) // export module.exports = routerCopy the code
    • controller

Let’s see what happens

Start by creating a piece of data

And then query it


Deployment server code

First put the code into the server, and configure nginx, the method in the previous [Easy] front end to full stack from here.

//nginx adds configuration location/API {proxy_pass http://127.0.0.1:8989; }Copy the code
// install forever NPM install forever -g # install forever start app.js # start app forever stop app.js # close app forever restartall # restartall appsCopy the code

The last

Here is probably a simple server through

Attach code addresshttps://gitee.com/wisdom_QQ/koa

Remember to like !!!!!!!!!!!

Remember to like !!!!!!!!!!!