• Stars
    star
    1,358
  • Rank 33,293 (Top 0.7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A MySQL-compatible relational database with a storage agnostic query engine. Implemented in pure Go.

A MySQL compatible database engine written in pure Go

go-mysql-server is a data-source agnostic SQL engine and server which runs queries on data sources you provide, using the MySQL dialect and wire protocol. A simple in-memory database implementation is included, and you can query any data source you want by implementing your own backend.

Dolt, a SQL database with Git-style versioning, is the main production database implementation of this package. Check out that project for reference a implementation. Or, hop into the Dolt discord here if you want to talk to the core developers behind go-mysql-server and Dolt.

Compatibility

With the exception of specific limitations (see below), go-mysql-server is a drop-in replacement for MySQL. Any client library, tool, query, SQL syntax, SQL function, etc. that works with MySQL should also work with go-mysql-server. If you find a gap in functionality, please file an issue.

For full MySQL compatibility documentation, see the Dolt docs on this topic.

Scope of this project

  • SQL server and engine to query your data sources.
  • In-memory database backend implementation suitable for use in tests.
  • Interfaces you can use to implement new backends to query your own data sources.
  • With a few caveats and using a full database implementation, a drop-in MySQL database replacement.

go-mysql-server has two primary uses case:

  1. Stand-in for MySQL in a golang test environment, using the built-in memory database implementation.

  2. Providing access to aribtrary data sources with SQL queries by implementing a handful of interfaces. The most complete real-world implementation is Dolt.

Installation

Add go-mysql-server as a dependency to your project. In the directory with the go.mod file, run:

go get github.com/dolthub/go-mysql-server@latest

Using the in-memory test server

The in-memory test server can replace a real MySQL server in tests. Start the server using the code in the _example directory, also reproduced below.

package main

import (
	"fmt"
	"time"

	sqle "github.com/dolthub/go-mysql-server"
	"github.com/dolthub/go-mysql-server/memory"
	"github.com/dolthub/go-mysql-server/server"
	"github.com/dolthub/go-mysql-server/sql"
	"github.com/dolthub/go-mysql-server/sql/types"
)

var (
	dbName    = "mydb"
	tableName = "mytable"
	address   = "localhost"
	port      = 3306
)

func main() {
	ctx := sql.NewEmptyContext()
	engine := sqle.NewDefault(
		memory.NewDBProvider(
			createTestDatabase(ctx),
		))

	// This variable may be found in the "users_example.go" file. Please refer to that file for a walkthrough on how to
	// set up the "mysql" database to allow user creation and user checking when establishing connections. This is set
	// to false for this example, but feel free to play around with it and see how it works.
	if enableUsers {
		if err := enableUserAccounts(ctx, engine); err != nil {
			panic(err)
		}
	}

	config := server.Config{
		Protocol: "tcp",
		Address:  fmt.Sprintf("%s:%d", address, port),
	}
	s, err := server.NewDefaultServer(config, engine)
	if err != nil {
		panic(err)
	}
	if err = s.Start(); err != nil {
		panic(err)
	}
}

func createTestDatabase(ctx *sql.Context) *memory.Database {
	db := memory.NewDatabase(dbName)
	db.EnablePrimaryKeyIndexes()
	table := memory.NewTable(tableName, sql.NewPrimaryKeySchema(sql.Schema{
		{Name: "name", Type: types.Text, Nullable: false, Source: tableName, PrimaryKey: true},
		{Name: "email", Type: types.Text, Nullable: false, Source: tableName, PrimaryKey: true},
		{Name: "phone_numbers", Type: types.JSON, Nullable: false, Source: tableName},
		{Name: "created_at", Type: types.Datetime, Nullable: false, Source: tableName},
	}), db.GetForeignKeyCollection())
	db.AddTable(tableName, table)

	creationTime := time.Unix(0, 1667304000000001000).UTC()
	_ = table.Insert(ctx, sql.NewRow("Jane Deo", "[email protected]", types.MustJSON(`["556-565-566", "777-777-777"]`), creationTime))
	_ = table.Insert(ctx, sql.NewRow("Jane Doe", "[email protected]", types.MustJSON(`[]`), creationTime))
	_ = table.Insert(ctx, sql.NewRow("John Doe", "[email protected]", types.MustJSON(`["555-555-555"]`), creationTime))
	_ = table.Insert(ctx, sql.NewRow("John Doe", "[email protected]", types.MustJSON(`[]`), creationTime))
	return db
}

This example populates the database by creating memory.Database and memory.Table objects via golang code, but you can also populate it by issuing CREATE DATABASE, CREATE TABLE, etc. statements to the server once it's running.

Once the server is running, connect with any MySQL client, including the golang MySQL connector and the mysql shell.

> mysql --host=localhost --port=3306 --user=root mydb --execute="SELECT * FROM mytable;"
+----------+-------------------+-------------------------------+----------------------------+
| name     | email             | phone_numbers                 | created_at                 |
+----------+-------------------+-------------------------------+----------------------------+
| Jane Deo | [email protected] | ["556-565-566","777-777-777"] | 2022-11-01 12:00:00.000001 |
| Jane Doe | [email protected]      | []                            | 2022-11-01 12:00:00.000001 |
| John Doe | [email protected]      | ["555-555-555"]               | 2022-11-01 12:00:00.000001 |
| John Doe | [email protected]   | []                            | 2022-11-01 12:00:00.000001 |
+----------+-------------------+-------------------------------+----------------------------+

Limitations of the in-memory database implementation

The in-memory database implementation included with this package is intended for use in tests. It has specific limitations that we know of:

Custom backend implementations

You can create your own backend to query your own data sources by implementing some interfaces. For detailed instructions, see the backend guide.

Technical documentation for contributors and backend developers

  • Architecture is an overview of the various packages of the project and how they fit together.
  • Contribution guide for new contributors, including instructions for how to get your PR merged.

Powered by go-mysql-server

Are you building a database backend using go-mysql-server? We would like to hear from you and include you in this list.

Acknowledgements

go-mysql-server was originally developed by the {source-d} organzation, and this repository was originally forked from src-d. We want to thank the entire {source-d} development team for their work on this project, especially Miguel Molina (@erizocosmico) and Juanjo รlvarez Martinez (@juanjux).

License

Apache License 2.0, see LICENSE

More Repositories

1

dolt

Dolt โ€“ Git for Data
Go
16,618
star
2

doltgresql

DoltgreSQL - Version Controlled PostgreSQL
Go
818
star
3

swiss

Golang port of Abseil's SwissTable
Go
435
star
4

maphash

Go
76
star
5

jsplit

A Go program to split large JSON files into many jsonl files
Go
54
star
6

doltpy

A Python API for Dolt
Python
53
star
7

data-analysis

Jupyter Notebook
43
star
8

pg2mysql

Script to convert Postgres dump files to MySQL dump files
Shell
31
star
9

dolthub-etl-jobs

ETL jobs that DoltHub maintained that load public data into DoltHub.
Python
17
star
10

dolthub-cypress

A suite of Cypress tests that runs against DoltHub (dolthub.com)
TypeScript
13
star
11

driver

Golang database/sql driver for Dolt
Go
12
star
12

bounties

Go
9
star
13

doltcli

Python
8
star
14

go-icu-regex

ICU Regular Expressions in Go
Go
8
star
15

aws-incident-manager-oncall-calendar-generator

Go
7
star
16

docs

Shell
6
star
17

grammar-crawler

Java
6
star
18

dolt-action

Shell
6
star
19

dolthub-issues

Issues for dolthub.com
4
star
20

doltlab-issues

Issue tracking for DoltLab
4
star
21

kedro-dolt

Kedro-Dolt Hook Plugin
Python
4
star
22

fuzzer

Fuzzer for Dolt repositories
Go
3
star
23

gsheets-to-csv

Python
3
star
24

dolt-integrations

Python
3
star
25

chat-bot-profanity-filter

Simple chat bot with a profanity filter built with data from https://www.dolthub.com/repositories/Liquidata/bad-words
Python
3
star
26

insert-batcher

Go
2
star
27

go-library-sample

This is a sample C++ application showing how to embed a Go library into C/C++
Go
2
star
28

hosted-issues

Issues for hosted.doltdb.com
2
star
29

gsheets-sync-demo

1
star
30

windows-msi-creator

One stop shop for creating Windows MSI binaries for Dolt releases.
Batchfile
1
star
31

dolt-wordpress

Wordpress plugin w/ Dolt
1
star
32

efcore-sample

Sample project using .NET Entity Framework Core to access a Dolt database
C#
1
star
33

ai-dungeon-scraper

Python
1
star
34

read-replication-demo

Shell
1
star
35

release-notes-generator

Automated release notes generation for GitHub project releases
Perl
1
star
36

query-faq-toy

Go
1
star
37

hosted-cypress

A suite of Cypress tests that runs against Hosted DoltDB (hosted.doltdb.com)
TypeScript
1
star
38

cold-marketing

cold email marketing emails
1
star
39

dolt-rest-example

Python
1
star
40

label-customer-issues

A GitHub Action to apply a label to issues and PRs created by authors who are not members of the team that owns a repo.
JavaScript
1
star