当前位置:网站首页>The practice of beego framework developed by goweb: Section 4 database configuration and connection
The practice of beego framework developed by goweb: Section 4 database configuration and connection
2022-07-28 17:20:00 【qfliweimin】
In the previous section, I summarized beego frame , This lesson continues to learn database configuration and ORM Introduction to mapping relationship
mysql Database installation
mysql Official download site :https://dev.mysql.com/downloads/ We use 5.7 edition , Download link :https://dev.mysql.com/downloads/mysql/5.7.html#downloads

Select download installation file , And install locally . During installation , If the initial password appears , Remember the password , Use when logging in for the first time .
【 Be careful 】windows Installation instructions :https://jingyan.baidu.com/article/09ea3ededa53b7c0aede39b5.html
After installation , take mysql Of bin Directory path add configuration to environment variable , In order to be able to use login in the terminal command line mysql.
Log in to the terminal mysql The order of :
mysql -root root -pEnter the password set during installation or yourself to log in successfully . As shown below :

As shown in the above figure, it indicates that the login is successful . In order to use your own password , You can set a password you are familiar with , Make it easy to remember .
mysql Database common commands
mysql Some commands of the database use :
- view the database :
show databases; - Use a database :
use databaseName; - Show a list of database tables :
show tables; - mysql Some other commands in :

above mysql Data operations are in the form of command line terminals , In order to facilitate our daily operation , We can use graphical interface tools , In this case we are using navicat Tools . Now let's see how to install .
Navicat install
navicat Tool download address :https://www.navicat.com/en/download/navicat-for-mysql
In the link above , Choose your own system version , Then download the installation file , Installation , Always choose the next step by default , Finally installed successfully .
After installation , stay mysql Create a new database in the database , For example, the name is :myblog; After creating the database , Let's take a look at beego How to use in a project mysql database .
Database driven
We told you before , Database programming belongs to beego Medium Models layer , Also known as ORM modular .
stay beego in , At present, three database drivers are supported , Namely :
- MySQL:http://github.com/go-sql-driver/mysql
- PostgreSQL:http://github.com/lib/pq
- Sqlite3:http://github.com/mattn/go-sqlite3
beego Medium ORM Several features :
- 1) Support Go All types of language storage
- 2)CRUD It's easy to operate
- 3) Automatically Join Association table
- 4) Allow direct use of SQL Inquire about
beego Project use mysql
- 1、 Import the corresponding database driver such as mysql:
import _ "http://github.com/go-sql-driver/mysql"

- 2、 Registration drive , Connect to database Register the database driver with the following two sentences , And the operation of connecting to the database :
orm.RegisterDriver("mysql",orm.DRMySQL)
orm.RegisterDataBase(aliasName,driverName,dbConn)
The detailed code is as follows :

- 3、 Create the database and execute the program

The connection code is as follows :
package models
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"BlogProject/MysqlDemo/util"
// Bear in mind : Import driver package
_ "github.com/go-sql-driver/mysql"
)
func init() {
driverName := beego.AppConfig.String("driverName")
// Register database driver
orm.RegisterDriver(driverName, orm.DRMySQL)
// Database connection
user := beego.AppConfig.String("mysqluser")
pwd := beego.AppConfig.String("mysqlpwd")
host := beego.AppConfig.String("host")
port := beego.AppConfig.String("port")
dbname := beego.AppConfig.String("dbname")
//dbConn := "root:[email protected](127.0.0.1:3306)/cmsproject?charset=utf8"
dbConn := user + ":" + pwd + "@tcp(" + host + ":" + port + ")/" + dbname + "?charset=utf8"
err := orm.RegisterDataBase("default", driverName, dbConn)
if err != nil {
util.LogError(" Error connecting to database ")
return
}
util.LogInfo(" Successfully connected to database ")
}

边栏推荐
- Rsync service deployment and parameter details
- The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
- How to use fail2ban to protect WordPress login page
- Self study examination in April 2021
- wpf命令按钮透明样式
- Use of influxdb2
- Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
- Goweb开发之Beego框架实战:第一节 Beego框架介绍
- 22年多校第三场(F的证明
- Comprehensively design an oppe homepage -- after sales service of the page
猜你喜欢

How to protect image security during construction

Proof of the third scene (f) in 22 years

Goweb开发之Beego框架实战:第三节 程序执行流程分析

mysql 最大建议行数2000w,靠谱吗?

Goweb开发之Beego框架实战:第一节 Beego框架介绍

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

Use of influxdb2

C# 导入Excel文件数据的几种方法

Application of Pegasus d200s UAV and airborne lidar in large-scale DEM construction

22年多校第三场(F的证明
随机推荐
Goweb开发之Beego框架实战:第一节 Beego框架介绍
valarray数值库学习
Algorithm learning: leetcode interview question 09. implement queue with two stacks
Ugui learning notes (I) rendering level
Problem solution of code heartstrings Junior Group (official competition) of Dalian University of Technology (Development Zone campus) in 2021
Easypoi --- excel file export
高速电路中电阻的选择
UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
System clock failure of database fault tolerance
Easypoi multi sheet export by template
How to protect image security during construction
Analysis of kubernetes service principle
Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low
Unity shader procedural texture
Codeforces Round #750 (Div. 2) F.Korney Korneevich and XOR (easy&&hard version)(dp)
2022牛客多校第二场CDE
Unity shader screen post-processing
[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)
部分情况下Error:(xx, xx) Failed to resolve: xxxxxx解决。
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market