github.com/dolthub/go-mysql-server@v0.18.0/README.md (about)

     1  <img height="240" src="./mascot.png"/>
     2  
     3  # A MySQL compatible database engine written in pure Go
     4  
     5  **go-mysql-server** is a data-source agnostic SQL engine and server
     6  which runs queries on data sources you provide, using the MySQL
     7  dialect and wire protocol. A simple in-memory database implementation
     8  is included, and you can query any data source you want by
     9  implementing your own backend.
    10  
    11  [Dolt](https://www.doltdb.com), a SQL database with Git-style
    12  versioning, is the main production database implementation of this
    13  package.  [Check
    14  out](https://docs.dolthub.com/introduction/what-is-dolt) that project
    15  for reference a implementation. Or, hop into the Dolt discord
    16  [here](https://discord.com/invite/RFwfYpu) if you want to talk to the
    17  [core developers](https://www.dolthub.com/team) behind
    18  **go-mysql-server** and Dolt.
    19  
    20  ## Compatibility
    21  
    22  With the exception of specific limitations (see below),
    23  **go-mysql-server** is a drop-in replacement for MySQL. Any client
    24  library, tool, query, SQL syntax, SQL function, etc. that works with
    25  MySQL should also work with **go-mysql-server**. If you find a gap in
    26  functionality, please file an issue.
    27  
    28  For full MySQL compatibility documentation, see the [Dolt
    29  docs](https://docs.dolthub.com/sql-reference/sql-support) on this
    30  topic.
    31  
    32  ## Scope of this project
    33  
    34  - SQL server and engine to query your data sources.
    35  - In-memory database backend implementation suitable for use in tests.
    36  - Interfaces you can use to implement new backends to query your own
    37    data sources.
    38  - With a few caveats and using a full database implementation, a
    39    drop-in MySQL database replacement.
    40  
    41  **go-mysql-server** has two primary uses case:
    42  
    43  1. Stand-in for MySQL in a golang test environment, using the built-in
    44     `memory` database implementation.
    45  
    46  2. Providing access to arbitrary data sources with SQL queries by
    47     implementing a handful of interfaces. The most complete real-world
    48     implementation is [Dolt](https://github.com/dolthub/dolt).
    49  
    50  ## Installation
    51  
    52  Add **go-mysql-server** as a dependency to your project. In the
    53  directory with the `go.mod` file, run:
    54  
    55  ```
    56  go get github.com/dolthub/go-mysql-server@latest
    57  ```
    58  
    59  ## Using the in-memory test server
    60  
    61  The in-memory test server can replace a real MySQL server in
    62  tests. Start the server using the code in the [_example
    63  directory](_example/main.go), also reproduced below.
    64  
    65  ```go
    66  
    67  package main
    68  
    69  import (
    70  	"fmt"
    71  	"time"
    72  
    73  	sqle "github.com/dolthub/go-mysql-server"
    74  	"github.com/dolthub/go-mysql-server/memory"
    75  	"github.com/dolthub/go-mysql-server/server"
    76  	"github.com/dolthub/go-mysql-server/sql"
    77  	"github.com/dolthub/go-mysql-server/sql/types"
    78  )
    79  
    80  var (
    81  	dbName    = "mydb"
    82  	tableName = "mytable"
    83  	address   = "localhost"
    84  	port      = 3306
    85  )
    86  
    87  func main() {
    88  	ctx := sql.NewEmptyContext()
    89  	engine := sqle.NewDefault(
    90  		memory.NewDBProvider(
    91  			createTestDatabase(ctx),
    92  		))
    93  
    94  	// This variable may be found in the "users_example.go" file. Please refer to that file for a walkthrough on how to
    95  	// set up the "mysql" database to allow user creation and user checking when establishing connections. This is set
    96  	// to false for this example, but feel free to play around with it and see how it works.
    97  	if enableUsers {
    98  		if err := enableUserAccounts(ctx, engine); err != nil {
    99  			panic(err)
   100  		}
   101  	}
   102  
   103  	config := server.Config{
   104  		Protocol: "tcp",
   105  		Address:  fmt.Sprintf("%s:%d", address, port),
   106  	}
   107  	s, err := server.NewDefaultServer(config, engine)
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  	if err = s.Start(); err != nil {
   112  		panic(err)
   113  	}
   114  }
   115  
   116  func createTestDatabase(ctx *sql.Context) *memory.Database {
   117  	db := memory.NewDatabase(dbName)
   118  	db.EnablePrimaryKeyIndexes()
   119  	table := memory.NewTable(tableName, sql.NewPrimaryKeySchema(sql.Schema{
   120  		{Name: "name", Type: types.Text, Nullable: false, Source: tableName, PrimaryKey: true},
   121  		{Name: "email", Type: types.Text, Nullable: false, Source: tableName, PrimaryKey: true},
   122  		{Name: "phone_numbers", Type: types.JSON, Nullable: false, Source: tableName},
   123  		{Name: "created_at", Type: types.Datetime, Nullable: false, Source: tableName},
   124  	}), db.GetForeignKeyCollection())
   125  	db.AddTable(tableName, table)
   126  
   127  	creationTime := time.Unix(0, 1667304000000001000).UTC()
   128  	_ = table.Insert(ctx, sql.NewRow("Jane Deo", "janedeo@gmail.com", types.MustJSON(`["556-565-566", "777-777-777"]`), creationTime))
   129  	_ = table.Insert(ctx, sql.NewRow("Jane Doe", "jane@doe.com", types.MustJSON(`[]`), creationTime))
   130  	_ = table.Insert(ctx, sql.NewRow("John Doe", "john@doe.com", types.MustJSON(`["555-555-555"]`), creationTime))
   131  	_ = table.Insert(ctx, sql.NewRow("John Doe", "johnalt@doe.com", types.MustJSON(`[]`), creationTime))
   132  	return db
   133  }
   134  ```
   135  
   136  This example populates the database by creating `memory.Database` and
   137  `memory.Table` objects via golang code, but you can also populate it
   138  by issuing `CREATE DATABASE`, `CREATE TABLE`, etc. statements to the
   139  server once it's running.
   140  
   141  Once the server is running, connect with any MySQL client, including
   142  the golang MySQL connector and the `mysql` shell.
   143  
   144  ```bash
   145  > mysql --host=localhost --port=3306 --user=root mydb --execute="SELECT * FROM mytable;"
   146  +----------+-------------------+-------------------------------+----------------------------+
   147  | name     | email             | phone_numbers                 | created_at                 |
   148  +----------+-------------------+-------------------------------+----------------------------+
   149  | Jane Deo | janedeo@gmail.com | ["556-565-566","777-777-777"] | 2022-11-01 12:00:00.000001 |
   150  | Jane Doe | jane@doe.com      | []                            | 2022-11-01 12:00:00.000001 |
   151  | John Doe | john@doe.com      | ["555-555-555"]               | 2022-11-01 12:00:00.000001 |
   152  | John Doe | johnalt@doe.com   | []                            | 2022-11-01 12:00:00.000001 |
   153  +----------+-------------------+-------------------------------+----------------------------+
   154  ```
   155  
   156  ## Limitations of the in-memory database implementation
   157  
   158  The in-memory database implementation included with this package is
   159  intended for use in tests. It has specific limitations that we know
   160  of:
   161  
   162  - [Not
   163    threadsafe](https://github.com/dolthub/go-mysql-server/issues/1306). To
   164    avoid concurrency issues, limit DDL and DML statements (`CREATE
   165    TABLE`, `INSERT`, etc.) to a single goroutine.
   166  - [No transaction
   167    support](https://github.com/dolthub/go-mysql-server/issues/1506). Statements
   168    like `START TRANSACTION`, `ROLLBACK`, and `COMMIT` are no-ops.
   169  - [Non-performant index
   170    implementation](https://github.com/dolthub/go-mysql-server/issues/1347). Indexed
   171    lookups and joins perform full table scans on the underlying tables.
   172  
   173  ## Custom backend implementations
   174  
   175  You can create your own backend to query your own data sources by
   176  implementing some interfaces. For detailed instructions, see the
   177  [backend guide](./BACKEND.md).
   178  
   179  ## Technical documentation for contributors and backend developers
   180  
   181  - [Architecture](./ARCHITECTURE.md) is an overview of the various
   182    packages of the project and how they fit together.
   183  - [Contribution guide](./CONTRIBUTING.md) for new contributors,
   184    including instructions for how to get your PR merged.
   185  
   186  ## Powered by go-mysql-server
   187  
   188  * [dolt](https://github.com/dolthub/dolt)
   189  * [gitbase](https://github.com/src-d/gitbase) (defunct)
   190  
   191  Are you building a database backend using **go-mysql-server**? We
   192  would like to hear from you and include you in this list.
   193  
   194  ## Security Policy
   195  
   196  [go-mysql-server's security
   197  policy](https://github.com/dolthub/go-mysql-server/blob/main/SECURITY.md) is
   198  maintained in this repository. Please follow the disclosure instructions there.
   199  Please do not initially report security issues in this repository's public
   200  GitHub issues.
   201  
   202  ## Acknowledgements
   203  
   204  **go-mysql-server** was originally developed by the `{source-d}`
   205  organzation, and this repository was originally forked from
   206  [src-d](https://github.com/src-d/go-mysql-server). We want to thank
   207  the entire `{source-d}` development team for their work on this
   208  project, especially Miguel Molina (@erizocosmico) and Juanjo Álvarez
   209  Martinez (@juanjux).
   210  
   211  ## License
   212  
   213  Apache License 2.0, see [LICENSE](/LICENSE)