A memorandum for learning Golang. I referred to the following URL this time.
Try using MariaDB Server from Go language Connect to MySQL with Go
If you like, please refer to the following. Install Golang on CentOS 8
Terminal
#Mariadb installation
dnf install -y mariadb mariadb-server
#MariaDB start / automatic start setting
systemctl start mariadb
systemctl enable mariadb
#Set the initial password for MariaDB root user
/usr/bin/mysql_secure_installation
#Set root password? [Y/n]y → select "y"
#New password:→ Input
#Re-enter new password:→ Re-enter
#Password updated successfully!
#Mariadb version check
mysql --version
#Version information is displayed
#If git is not installed, use the following command
#Install
dnf -y install git
#MariaDB does not provide a Connector for Go, so
#Use the MySQL driver published on GitHub
go get github.com/go-sql-driver/mysql
--Create Go module for connection
check_mariadb_ver.go
package main
import(
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main(){
//Connect to database
db, err := sql.Open("mysql", "User name:password@/Database name")
if err != nil {
fmt.Println(err.Error())
}
defer db.Close()
//Check version information
var version string
db.QueryRow("SELECT VERSION()").Scan(&version)
//Version information output
fmt.Println("connected to:", version)
}
--Module execution
Terminal
#Run
go run check_mariadb_ver.go
#Version information is output
connected to: 10.3.17-MariaDB
We've seen how to connect to Mariadb from Go and execute SQL. In the future, we will create APIs using Go, etc. Ultimately, I would like to develop a simple application.
Recommended Posts