photoprism/internal/tidb/init.go

52 lines
963 B
Go
Raw Normal View History

package tidb
import (
"database/sql"
"fmt"
log "github.com/sirupsen/logrus"
)
func InitDatabase(port uint, password string) error {
2019-06-30 00:21:02 +02:00
log.Info("init database")
db, err := sql.Open("mysql", fmt.Sprintf("root:@tcp(localhost:%d)/", port))
defer db.Close()
if err != nil {
log.Debug(err.Error())
log.Debug("database login as root with password")
db, err = sql.Open("mysql", fmt.Sprintf("root:%s@tcp(localhost:%d)/", password, port))
}
if err != nil {
log.Error(err.Error())
return err
}
log.Debug("set database password")
_, err = db.Exec(fmt.Sprintf("SET PASSWORD FOR 'root'@'%%' = '%s'", password))
if err != nil {
log.Error(err.Error())
} else {
log.Debug("flush database privileges")
_, err = db.Exec("FLUSH PRIVILEGES")
}
log.Debug("create database if not exists")
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS photoprism")
if err != nil {
log.Error(err.Error())
}
log.Info("database created")
return nil
}