• Stars
    star
    106
  • Rank 314,915 (Top 7 %)
  • Language
    Crystal
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

MySQL connector for Crystal

crystal-mysql Build Status

MySQL driver implement natively in Crystal, without relying on external libraries.

Check crystal-db for general db driver documentation. crystal-mysql driver is registered under mysql:// uri.

Why

Using a natively implemented library has a significant performance improvement over working with an external library, since there is no need to copy data to and from the Crystal space and the native code. Initial tests with the library have shown a 2x-3x performance boost, though additional testing is required.

Also, going through the MySQL external library blocks the Crystal thread using it, thus imposing a significant penalty to concurrent database accesses, such as those in web servers. We aim to overcome this issue through a full Crystal implementation of the MySQL driver that plays nice with non-blocking IO.

Status

This driver is a work in progress. It implements mysql's binary protocol to create prepared statements. Contributions are most welcome.

Installation

Add this to your application's shard.yml:

dependencies:
  mysql:
    github: crystal-lang/crystal-mysql

Usage

require "mysql"

# connect to localhost mysql test db
DB.open "mysql://root@localhost/test" do |db|
  db.exec "drop table if exists contacts"
  db.exec "create table contacts (name varchar(30), age int)"
  db.exec "insert into contacts values (?, ?)", "John Doe", 30

  args = [] of DB::Any
  args << "Sarah"
  args << 33
  db.exec "insert into contacts values (?, ?)", args: args

  puts "max age:"
  puts db.scalar "select max(age) from contacts" # => 33

  puts "contacts:"
  db.query "select name, age from contacts order by age desc" do |rs|
    puts "#{rs.column_name(0)} (#{rs.column_name(1)})"
    # => name (age)
    rs.each do
      puts "#{rs.read(String)} (#{rs.read(Int32)})"
      # => Sarah (33)
      # => John Doe (30)
    end
  end
end

When running this example, if you get the following exception:

Unhandled exception: Client does not support authentication protocol requested by server; consider upgrading MySQL client (Exception)

You have two options, set a password for root, or (most recommended option) create another user with access to test database.

CREATE USER 'test'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
quit

Then use the example above changing the DB.open line to

DB.open "mysql://test:yourpassword@localhost/test" do |db|

Connection URI

The connection string has the following syntax:

mysql://[user[:[password]]@]host[:port][/schema][?param1=value1&param2=value2]

Connection query params:

  • encoding: The collation & charset (character set) to use during the connection. If empty or not defined, it will be set to utf8_general_ci. The list of available collations is defined in MySql::Collations::COLLATIONS_IDS_BY_NAME

More Repositories

1

crystal

The Crystal Programming Language
Crystal
19,110
star
2

shards

Dependency manager for the Crystal language
Crystal
753
star
3

crystal-book

Crystal reference with language specification, manuals and learning materials
Makefile
384
star
4

crystal-db

Common db api for crystal
Crystal
297
star
5

crystal_lib

Automatic binding generator for native libraries in Crystal
Crystal
139
star
6

crystal-sqlite3

SQLite3 bindings for Crystal
Crystal
131
star
7

heroku-buildpack-crystal

Heroku buildpack for Crystal
Shell
123
star
8

install-crystal

GitHub Action: Install Crystal programming language
JavaScript
67
star
9

html_builder

DSL for creating HTML
Crystal
62
star
10

crystal-website

crystal-lang.org website
SCSS
56
star
11

clang.cr

libclang bindings for crystal (including automatic C bindings generator)
Crystal
46
star
12

crystal-presents

Playground for creating crystal presentations with live coding
CSS
40
star
13

distribution-scripts

Shell
39
star
14

crystal-molinillo

A generic dependency resolution algorithm. Ported from https://github.com/CocoaPods/Molinillo/
Crystal
29
star
15

omnibus-crystal

Omnibus builder for Crystal
Ruby
25
star
16

perf-tools

An assortment of tools to track resources in Crystal applications
Crystal
23
star
17

bootstrap-script

Automated script to bootstrap the crystal compiler from source
Shell
22
star
18

crystal-readline

Crystal bindings to GNU Readline Library
Crystal
21
star
19

json_mapping.cr

Crystal
18
star
20

rfcs

RFCs repository for Crystal
14
star
21

crystal-env

Crystal
13
star
22

logger.cr

Crystal
13
star
23

homebrew-crystal

Homebrew Tap for Crystal development
Ruby
13
star
24

yaml_mapping.cr

Crystal
8
star
25

crystal-dist

Docker workspace to sign and release Crystal binaries to repositories
Shell
7
star
26

test-ecosystem

Shell
6
star
27

crystal-infrastructure

Infrastructure automation for Crystal related resources
Jinja
4
star
28

crystal-random

Crystal
3
star
29

osc-docker

Dockerfile for pushing to Open Build Service
Dockerfile
1
star
30

.github

Default community health files
1
star