{"id":15449465,"url":"https://github.com/hooopo/tidb-serverless-ruby-connect-example","last_synced_at":"2026-05-11T06:02:46.619Z","repository":{"id":175437279,"uuid":"653821304","full_name":"hooopo/tidb-serverless-ruby-connect-example","owner":"hooopo","description":"TiDB serverless ruby connect example","archived":false,"fork":false,"pushed_at":"2023-06-15T01:51:55.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-18T16:22:47.036Z","etag":null,"topics":["mysql","rails","ruby","tidb"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hooopo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-14T20:12:32.000Z","updated_at":"2024-04-22T08:48:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"7f40b96d-110a-4bc2-a555-44c1747342e8","html_url":"https://github.com/hooopo/tidb-serverless-ruby-connect-example","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":"0.18181818181818177","last_synced_commit":"f659f93a79482b085c5a61d37e69b91bb04e43cc"},"previous_names":["hooopo/tidb-serverless-ruby-connect-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hooopo%2Ftidb-serverless-ruby-connect-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hooopo%2Ftidb-serverless-ruby-connect-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hooopo%2Ftidb-serverless-ruby-connect-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hooopo%2Ftidb-serverless-ruby-connect-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hooopo","download_url":"https://codeload.github.com/hooopo/tidb-serverless-ruby-connect-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245999607,"owners_count":20707570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","rails","ruby","tidb"],"created_at":"2024-10-01T21:00:39.782Z","updated_at":"2026-05-11T06:02:41.573Z","avatar_url":"https://github.com/hooopo.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TiDB Serverless Ruby Connect Example\n\nIn this example, we will demonstrate how to connect to a TiDB Serverless instance using Ruby and [mysql2](https://github.com/brianmario/mysql2) gem and rails.\n\n### mysql2 \n\nFirst, please make sure you have Ruby and the mysql2 gem installed.\n\n```bash\n$ ruby -v\n$ bundle install\n```\n\nThen, you can run the example code to connect to a TiDB Serverless instance using mysql2 gem.\n\n```bash\n$ TIDB_HOST=\u003ctidb-serverless-host\u003e TIDB_PORT=\u003ctidb-serverless-port\u003e TIDB_USERNAME=\u003ctidb-serverless-username\u003e TIDB_PASSWORD=\u003ctidb-serverless-password\u003e ruby mysql2.rb  \n```\n\nThe code in `mysql2.rb` is as follows:\n\n```ruby\nrequire 'mysql2'\n\nHOST = ENV['TIDB_HOST']\nPORT = ENV['TIDB_PORT']\nUSERNAME = ENV['TIDB_USERNAME']\nPASSWORD = ENV['TIDB_PASSWORD']\nDATABASE = ENV['TIDB_DATABASE'] || 'information_schema'\n\nputs \"Connecting to the TiDB Serverless instance...\"\nclient = Mysql2::Client.new(\n  host: HOST,\n  port: PORT,\n  username: USERNAME,\n  password: PASSWORD,\n  database: DATABASE,\n  ssl_mode: :verify_identity\n)\n\nputs \"Connected successfully.\"\n\nresp = client.query('select version()')\nputs resp.first\n\n# Close the client connection\nputs \"Closing the client connection...\"\nclient.close\nputs \"Connection closed.\"\n```\n\nWhen it connects successfully, you will see the following output:\n\n```bash\nConnecting to the TiDB Serverless instance...\nConnected successfully.\n{\"version()\"=\u003e\"5.7.25-TiDB-v6.6.0-serverless\"}\nClosing the client connection...\nConnection closed.\n```\n\nIt's essential to note that the best practice for establishing SSL connections using the mysql2 gem is to set `ssl_mode` to `verify_identity` and set `sslca` to `nil`. By default, the mysql2 gem will search for existing CA certificates in a particular order until a file is discovered.\n\n1. /etc/ssl/certs/ca-certificates.crt # Debian / Ubuntu / Gentoo / Arch / Slackware\n2. /etc/pki/tls/certs/ca-bundle.crt # RedHat / Fedora / CentOS / Mageia / Vercel / Netlify\n3. /etc/ssl/ca-bundle.pem # OpenSUSE\n4. /etc/ssl/cert.pem # MacOS / Alpine (docker container)\n\nWhile it is possible to specify the CA certificate path manually, this approach may cause significant inconvenience in multi-environment deployment scenarios, as different machines and environments may store the CA certificate in varying locations. Therefore, setting `sslca` to `nil` is recommended for flexibility and ease of deployment across different environments.\n\n```ruby\nclient = Mysql2::Client.new(\n  host: HOST,\n  port: PORT,\n  username: USERNAME,\n  password: PASSWORD,\n  database: DATABASE,\n  ssl_mode: :verify_identity,\n  sslca: '/path/to/ca-certificates.crt'\n)\n```\n\n### Rails\n\nIf you are using Rails, you can configure the database connection in `config/database.yml` as follows:\n\n```yaml\n# Install the MySQL driver\n#   gem install mysql2\n#\n# Ensure the MySQL gem is defined in your Gemfile\n#   gem \"mysql2\"\n\ndefault: \u0026default\n  adapter: mysql2\n  encoding: utf8mb4\n  pool: \u003c%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %\u003e\n  url: \u003c%= ENV['DATABASE_URL'] %\u003e\n  ssl_mode: :verify_identity\n\ndevelopment:\n  \u003c\u003c: *default\n  database: your_database_name\n```\n\nMake sure `ssl_mode` is set to `verify_identity` to enable SSL connections. Also, it is best practice to set `sslca` to `nil` or ignore it in the `config/database.yml` file. \n\nMore information about the ca certificate path to connect to TiDB Serverless can be found [here](https://docs.pingcap.com/tidbcloud/secure-connections-to-serverless-tier-clusters)\n\n\n## Need help?\n\nIf you have any questions about TiDB Serverless, please join the [TiDB community](https://ask.pingcap.com/) and feel free to ask for help.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhooopo%2Ftidb-serverless-ruby-connect-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhooopo%2Ftidb-serverless-ruby-connect-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhooopo%2Ftidb-serverless-ruby-connect-example/lists"}