Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClickHouse MySQL protocol does not work with Golang MySQL driver #64071

Closed
olegkv opened this issue May 17, 2024 · 5 comments
Closed

ClickHouse MySQL protocol does not work with Golang MySQL driver #64071

olegkv opened this issue May 17, 2024 · 5 comments

Comments

@olegkv
Copy link
Contributor

olegkv commented May 17, 2024

I use MySQL Go driver
https://github.com/go-sql-driver/mysql
to connect to ClickHouse.Cloud using MySQL port and protocol.
I get error:
"[mysql] connection.go:49: unexpected EOF
invalid connection"
So, I am not able to connect and run any query.

The code I use is this:
cfg := mysql.Config{
User: mysql_user,
Passwd: pwd,
Net: "tcp",
Addr: host,
DBName: database,
AllowNativePasswords: true,
Params: map[string]string{"parseTime": "True"},
}
connString := cfg.FormatDSN()
conn, err := sql.Open("mysql", connString)
conn.QueryRow("select 1")

My understanding is that ClickHouse should look like MySQL for applications connecting using Go driver, that's the point of the feature, so that existing apps can be re-pointed to ClickHouse from MySQL.

@olegkv olegkv added the potential bug To be reviewed by developers and confirmed/rejected. label May 17, 2024
@nikitamikhaylov
Copy link
Member

Please provide the full reproducer (the whole Go program) and step-by-step guidance how to get this error. This will simplify the work for developers and increase chances for it to be fixed quickly.

@olegkv
Copy link
Contributor Author

olegkv commented May 18, 2024

ch-test.zip
attached sample Go program
Just run it in debugger and see the error when query "select 1" is run

@alexey-milovidov alexey-milovidov added unexpected behaviour and removed potential bug To be reviewed by developers and confirmed/rejected. labels May 19, 2024
@slvrtrn
Copy link
Contributor

slvrtrn commented May 20, 2024

The MySQL interface is mainly for specific tools that don't have native ClickHouse support yet. We also have the official ClickHouse Go driver available (clickhouse-go).

In any case, if you grab the Let's Encrypt root cert, the following will work:

package main

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"database/sql"
	"fmt"
	"log"
	"os"

	"github.com/go-sql-driver/mysql"
)
func main() {
	ctx := context.Background()

	rootCertPool := x509.NewCertPool()
	pem, err := os.ReadFile("./isrgrootx1.pem")
	if err != nil {
		panic(err)
	}
	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
		log.Fatal("Failed to append PEM.")
	}
	err = mysql.RegisterTLSConfig("custom", &tls.Config{
		ServerName: "<service>.clickhouse.cloud",
		RootCAs:    rootCertPool,
	})
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("mysql", "<username>:<password>@tcp(<service>.clickhouse.cloud:3306)/default?tls=custom")
	if err != nil {
		panic(err)
	}

	conn, _ := db.Conn(ctx)
	var result string
	err = conn.QueryRowContext(ctx, `SELECT version() AS result`).Scan(&result)
	if err != nil {
		panic(err)
	}
	fmt.Printf("result: %s\n", result)
}

Which prints the correct result for my test instance:

result: 24.1.2.10900

Don't forget to allow the instance access for your IP address; otherwise, you might encounter the same EOF error again.

@olegkv
Copy link
Contributor Author

olegkv commented May 20, 2024

Hi guys
I thought that ClickHouse MySQL interface was intended to allow existing apps which use MySQL to use ClickHouse as drop-in replacement - just switch to new address, assuming that table structures are the same and SQL queries are compatible.
That is not the case?
ClickHouse documentation says:
"ClickHouse supports the MySQL wire protocol. This allow tools that are MySQL-compatible to interact with ClickHouse seamlessly"
I thought that means all apps which use MySQL? If that is not the case, please clarify.
Also, another example, I cannot connect to ClickHouse's MySQL port using DBeaver in same way as I connect to real MySQL database - is that expected behavior?
Thanks!

@slvrtrn
Copy link
Contributor

slvrtrn commented May 20, 2024

DBeaver has native ClickHouse support via the official JDBC driver — why would you use the MySQL interface for that? The same is true for DataGrip.

Regarding the intended use case, our primary focus was on the (proprietary) BI tools that don't have a native plugin or driver, such as:

  • Looker Studio
  • Tableau Online
  • QuickSight

When possible, using native drivers is always preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants