Glang+Vue+Mysql+Nginx实现前后端分离实例(二)
注意:本实例基于上一个实验:
实验准备
实验名称:基于Golang+Vue+Nginx搭建图书管理系统
实验环境:两台Centos7虚拟机
1、后端:node1,ip:192.168.141.53
2、前端:node2,ip:192.168.141.69
实验步骤
1、在上一个实验(Glang+Vue+Nginx实现前后端分离实例(一))中,在node2中部署了nginx实现了反向代理,在node1中安装设置好了go及其环境变量,两台虚拟机均放行了相应端口。本次实验将在上一次实验的node1、node2上继续进行。
2、node1安装mysql数据库,并且进行初始化,然后创建我们所需要用到的数据库和数据表
[root@node1 ~]# yum -y install mariadb mariadb-server
[root@node1 ~]# systemctl restart mariadb.service
[root@node1 ~]# systemctl enable mariadb.service
[root@node1 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@node1 ~]# firewall-cmd --reload
success
[root@node1 ~]#
[root@node1 ~]# mysql_secure_installation
#初始化数据库,设置root密码为:123456
[root@node1 ~]# mysql -u root -p
Enter password: #123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE sql_books;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
MariaDB [(none)]>
MariaDB [(none)]> use sql_books;
Database changed
MariaDB [sql_books]> CREATE TABLE `books` (
-> `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
-> `isbn` VARCHAR(40) DEFAULT '',
-> `title` VARCHAR(40) DEFAULT '',
-> `author_firstname` VARCHAR(20) DEFAULT '',
-> `author_lastname` VARCHAR(20) DEFAULT '',
-> PRIMARY KEY(`id`)
-> )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.01 sec)
MariaDB [sql_books]> exit
Bye
[root@node1 ~]#
3、node1重新编辑运行后端rest.go文件
[root@node1 ~]# cd /home/gopath/
[root@node1 gopath]# yum install git -y
[root@node1 gopath]# go get github.com/go-sql-driver/mysql
[root@node1 gopath]# vim rest.go
[root@node1 gopath]# cat rest.go
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"time"
)
//Book Struct
type Book struct{
ID int `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Author Author `json:"author"`
}
//Author Struct
type Author struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
// 定义一个全局对象db
var db *sql.DB
// 定义一个初始化数据库的函数
func initDB() (err error) {
// DSN:Data Source Name
dsn := "root:123456@tcp(192.168.141.53:3306)/sql_books"
// 不会校验账号密码是否正确
// 注意!!!这里不要使用:=,我们是给全局变量赋值,然后在main函数中使用全局变量db
db, err = sql.Open("mysql", dsn)
if err != nil {
return err
}
// 尝试与数据库建立连接(校验dsn是否正确)
err = db.Ping()
if err != nil {
return err
}
return nil
}
// 预处理查询单行示例
func prepareSingleQueryDemo(id int) (book Book){
sqlStr := "select id, isbn, title,author_firstname,author_lastname from books where id = ?"
stmt, err := db.Prepare(sqlStr)
if err != nil {
fmt.Printf("prepare failed, err:%v\n", err)
return
}
row, err:=stmt.Query(id)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
defer row.Close()
var b Book
for row.Next() {
err := row.Scan(&b.ID, &b.Isbn, &b.Title,&b.Author.Firstname,&b.Author.Lastname)
if err != nil