Install Golang / MariaDB on CentOS8, connect to DB and execute SQL

Overview

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

Golang installation / operation check

If you like, please refer to the following. Install Golang on CentOS 8

MariaDB installation / Preparing to connect from Go to Mariadb

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

Summary

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

Install Golang / MariaDB on CentOS8, connect to DB and execute SQL
How to install MariaDB 10.4 on CentOS 8
Steps to install samba on CentOS 8 and connect from Windows 10 Explorer
Install Golang on CentOS 8
How to install PHP 7.4 and SQL Server drivers in CentOS 7.7
Steps to install MySQL 8 on CentOS 8
Steps to install devtoolset-6 on CentOS 7
Install Java 9 on windows 10 and CentOS 7
How to install beta php8.0 on CentOS8
Install MariaDB (CentOS 8)
Command to install nginx / PHP7 / php-fpm on CentOS7
Reference articles and books used to install CentOS
Install Neo4j 4.1.3 on centOS
Install Vertica 10.0 on CentOS 6.10
Steps to set up Postfix and Dovecot on CentOS 8.3
Install PostgreSQL 12 on Centos8
Install nginx on centOS7
Install Python 3 on CentOS 7
Install kuromoji on CentOS7
Install Mattermost on CentOS 7
Install PostGIS 2.5.5 on CentOS7
Install jpndistrict on CentOS 7
Install MySQL 5.6 on CentOS6 [How to specify the version]
Install Redmine 4.1.1 on CentOS 7
Smokeping Install on CentOS7
Install PostgreSQL 13 on CentOS 7.5
How to Install Elixir and Phoenix Framework on Ubuntu 20.04 LTS
Install rsync on CentOS8 (version check) and file backup (synchronization)
How to install GNOME as a desktop environment on CentOS 7
Install OpenFOAM v2006 on CentOS
Install Jenkins on Docker's CentOS
Install Apache on CentOS on VirtualBox
Connect to DB with Java
Connect CentOS 7 to L2TP VPN
Install Ruby 2.7 on CentOS 7 (SCL)
How to install and use Composer on an ECS instance on Ubuntu 16.04
How to install and configure the monitoring tool "Graphite" on Ubuntu
Install Ubuntu20.04 on RaspberryPi 4 and build Kubernetes to run the container
Steps to install Maven on Mac and use it in Eclipse
Install Ubuntu Server 20.04 in VirtualBox on Mac and connect with SSH
Try DPDK20 SDK on CentOS7 ①Install
Install MariaDB on Raspberry Pi OS
How to deploy Laravel on CentOS 7
Enable Openjdk10 and maven on CentOS
Install JDK and JRE on Ubuntu 16.10
How to install ImageMagick on Windows 10
Install Ruby 2.5 on CentOS 7 using SCL
Install Java Open JDK 8 on CentOS 7
Install and configure jenv on macOS
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
How to connect Heroku and Sequel
Install apache 2.4.46 from source on CentOS7
How to install WildFly on Ubuntu 18.04
Challenge to install WSL2 and docker
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!