Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions docs/content/product/configuration/data-sources/mysql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,8 @@ When doing so, <EnvVar>CUBEJS_DB_HOST</EnvVar> will be ignored.

### Connecting to MySQL 8

MySQL 8 uses a new default authentication plugin (`caching_sha2_password`)
whereas previous version used `mysql_native_password`. Please
[see this answer on StackOverflow](https://stackoverflow.com/a/50377944)
for a workaround.

<InfoBox>

For additional details, check the [relevant issue](https://github.com/cube-js/cube/issues/3525) on GitHub.

</InfoBox>
MySQL 8 is fully supported and uses the new default authentication plugin
(`caching_sha2_password`). No additional configuration is required.

[mysql]: https://www.mysql.com/
[ref-caching-using-preaggs-build-strats]:
Expand Down
3 changes: 1 addition & 2 deletions packages/cubejs-mysql-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
"dependencies": {
"@cubejs-backend/base-driver": "1.6.17",
"@cubejs-backend/shared": "1.6.17",
"mysql": "^2.18.1"
"mysql2": "^3.16.1"
},
"devDependencies": {
"@cubejs-backend/linter": "1.6.17",
"@cubejs-backend/testing-shared": "1.6.17",
"@types/jest": "^29",
"@types/mysql": "^2.15.21",
"jest": "^29",
"stream-to-array": "^2.3.0",
"testcontainers": "^10.28.0",
Expand Down
54 changes: 34 additions & 20 deletions packages/cubejs-mysql-driver/src/MySqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
assertDataSource,
Pool,
} from '@cubejs-backend/shared';
import mysql, { Connection, ConnectionConfig, FieldInfo, QueryOptions } from 'mysql';
import mysql, { ConnectionOptions, QueryOptions } from 'mysql2';
import { promisify } from 'util';
import {
BaseDriver,
Expand Down Expand Up @@ -44,9 +44,9 @@ const MySqlNativeToMySqlType = {
[mysql.Types.INT24]: 'mediumint',
[mysql.Types.LONGLONG]: 'bigint',
[mysql.Types.NEWDATE]: 'datetime',
[mysql.Types.TIMESTAMP2]: 'timestamp',
[mysql.Types.DATETIME2]: 'datetime',
[mysql.Types.TIME2]: 'time',
[mysql.Types.TIMESTAMP]: 'timestamp',
[mysql.Types.DATETIME]: 'datetime',
[mysql.Types.TIME]: 'time',
[mysql.Types.TINY_BLOB]: 'tinytext',
[mysql.Types.MEDIUM_BLOB]: 'mediumtext',
[mysql.Types.LONG_BLOB]: 'longtext',
Expand All @@ -68,15 +68,20 @@ const MySqlToGenericType: Record<string, GenericDataBaseType> = {
'tinyint unsigned': 'int',
};

export interface MySqlDriverConfiguration extends ConnectionConfig {
export interface MySqlDriverConfiguration extends ConnectionOptions {
readOnly?: boolean,
loadPreAggregationWithoutMetaLock?: boolean,
storeTimezone?: string,
pool?: any,
}

interface MySQLConnection extends Connection {
execute: (options: string | QueryOptions, values?: any) => Promise<any>
interface MySQLConnection {
execute: (sql: string | QueryOptions, values?: any) => Promise<any>;
query: any;
end: any;
connect: any;
on: any;
destroy: any;
}

/**
Expand Down Expand Up @@ -124,7 +129,8 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
config.dataSource ||
assertDataSource('default');

const { pool, ...restConfig } = config;
const { pool, readOnly, ...restConfig } = config;
const sslOptions = this.getSslOptions(dataSource);
this.config = {
host: getEnv('dbHost', { dataSource }),
database: getEnv('dbName', { dataSource }),
Expand All @@ -133,14 +139,17 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
password: getEnv('dbPass', { dataSource }),
socketPath: getEnv('dbSocketPath', { dataSource }),
timezone: 'Z',
ssl: this.getSslOptions(dataSource),
ssl: sslOptions as any,
dateStrings: true,
readOnly: true,
decimalNumbers: true,
readOnly: readOnly !== undefined ? readOnly : true,
...restConfig,
};
this.pool = new Pool('mysql', {
create: async () => {
const conn: any = mysql.createConnection(this.config);
// Extract driver-specific options that mysql2 doesn't recognize
const { readOnly: _, loadPreAggregationWithoutMetaLock: __, storeTimezone: ___, ...connectionConfig } = this.config;
const conn: any = mysql.createConnection(connectionConfig);
const connect = promisify(conn.connect.bind(conn));

if (conn.on) {
Expand Down Expand Up @@ -319,13 +328,13 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
await this.setTimeZone(conn);

const [rowStream, fields] = await (
new Promise<[any, mysql.FieldInfo[]]>((resolve, reject) => {
new Promise<[any, mysql.FieldPacket[]]>((resolve, reject) => {
const stream = conn.query(query, values).stream({ highWaterMark });

stream.on('fields', (f) => {
stream.on('fields', (f: mysql.FieldPacket[]) => {
resolve([stream, f]);
});
stream.on('error', (e) => {
stream.on('error', (e: Error) => {
reject(e);
});
})
Expand All @@ -347,12 +356,17 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
}
}

protected mapFieldsToGenericTypes(fields: mysql.FieldInfo[]) {
return fields.map((field) => {
protected mapFieldsToGenericTypes(fields: mysql.FieldPacket[] | mysql.FieldPacket[][]) {
// mysql2 returns FieldPacket[] in callbacks
const fieldArray = Array.isArray(fields) && fields.length > 0 && Array.isArray(fields[0])
? fields[0] as mysql.FieldPacket[]
: fields as mysql.FieldPacket[];

return fieldArray.map((field) => {
// @ts-ignore
let dbType = mysql.Types[field.type];
let dbType = mysql.Types[field.type || 0];

if (field.type in MySqlNativeToMySqlType) {
if (field.type && field.type in MySqlNativeToMySqlType) {
// @ts-ignore
dbType = MySqlNativeToMySqlType[field.type];
}
Expand All @@ -373,13 +387,13 @@ export class MySqlDriver extends BaseDriver implements DriverInterface {
await this.setTimeZone(conn);

return new Promise((resolve, reject) => {
conn.query(query, values, (err, rows, fields) => {
conn.query(query, values, (err: any, rows: any, fields: any) => {
if (err) {
reject(err);
} else {
resolve({
rows,
types: this.mapFieldsToGenericTypes(<FieldInfo[]>fields),
types: this.mapFieldsToGenericTypes(fields as mysql.FieldPacket[]),
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DbRunnerAbstract, DBRunnerContainerOptions } from './db-runner.abstract

export class MysqlDBRunner extends DbRunnerAbstract {
public static startContainer(options: DBRunnerContainerOptions) {
const version = process.env.TEST_MYSQL_VERSION || options.version || '5.7';
const version = process.env.TEST_MYSQL_VERSION || options.version || '8.0';

const container = new GenericContainer(`mysql:${version}`)
.withEnvironment({
Expand All @@ -20,14 +20,6 @@ export class MysqlDBRunner extends DbRunnerAbstract {
.withWaitStrategy(Wait.forHealthCheck())
.withExposedPorts(3306);

if (version.split('.')[0] === '8') {
/**
* workaround for MySQL 8 and unsupported auth in mysql package
* @link https://github.com/mysqljs/mysql/pull/2233
*/
container.withCommand(['--default-authentication-plugin=mysql_native_password']);
}

if (options.volumes) {
const binds = options.volumes.map(v => ({ source: v.source, target: v.target, mode: v.bindMode }));
container.withBindMounts(binds);
Expand Down
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8772,7 +8772,7 @@
dependencies:
"@types/node" "*"

"@types/mysql@^2.15.15", "@types/mysql@^2.15.19", "@types/mysql@^2.15.21":
"@types/mysql@^2.15.15", "@types/mysql@^2.15.19":
version "2.15.21"
resolved "https://registry.yarnpkg.com/@types/mysql/-/mysql-2.15.21.tgz#7516cba7f9d077f980100c85fd500c8210bd5e45"
integrity sha512-NPotx5CVful7yB+qZbWtXL2fA4e7aEHkihHLjklc6ID8aq7bhguHgeIoC1EmSNTAuCgI6ZXrjt2ZSaXnYX0EUg==
Expand Down Expand Up @@ -19235,6 +19235,11 @@ lru.min@^1.0.0:
resolved "https://registry.yarnpkg.com/lru.min/-/lru.min-1.1.1.tgz#146e01e3a183fa7ba51049175de04667d5701f0e"
integrity sha512-FbAj6lXil6t8z4z3j0E5mfRlPzxkySotzUHwRXjlpRh10vc6AI6WN62ehZj82VG7M20rqogJ0GLwar2Xa05a8Q==

lru.min@^1.1.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/lru.min/-/lru.min-1.1.3.tgz#c8c3d001dfb4cbe5b8d1f4bea207d4a320e5d76f"
integrity sha512-Lkk/vx6ak3rYkRR0Nhu4lFUT2VDnQSxBe8Hbl7f36358p6ow8Bnvr8lrLt98H8J1aGxfhbX4Fs5tYg2+FTwr5Q==

luxon@^3.2.1:
version "3.4.4"
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.4.4.tgz#cf20dc27dc532ba41a169c43fdcc0063601577af"
Expand Down Expand Up @@ -20016,6 +20021,21 @@ mute-stream@^2.0.0:
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-2.0.0.tgz#a5446fc0c512b71c83c44d908d5c7b7b4c493b2b"
integrity sha512-WWdIxpyjEn+FhQJQQv9aQAYlHoNVdzIzUySNV1gHUPDSdZJ3yZn7pAAbQcV7B56Mvu881q9FZV+0Vx2xC44VWA==

mysql2@^3.11.0:
version "3.16.1"
resolved "https://registry.yarnpkg.com/mysql2/-/mysql2-3.16.1.tgz#cbe294573792fe4f059000f23d38e16011c83c61"
integrity sha512-b75qsDB3ieYEzMsT1uRGsztM/sy6vWPY40uPZlVVl8eefAotFCoS7jaDB5DxDNtlW5kdVGd9jptSpkvujNxI2A==
dependencies:
aws-ssl-profiles "^1.1.1"
denque "^2.1.0"
generate-function "^2.3.1"
iconv-lite "^0.7.0"
long "^5.2.1"
lru.min "^1.0.0"
named-placeholders "^1.1.6"
seq-queue "^0.0.5"
sqlstring "^2.3.2"

mysql2@^3.11.5:
version "3.11.5"
resolved "https://registry.yarnpkg.com/mysql2/-/mysql2-3.11.5.tgz#1a1cb9a61e78d636da10e122a3c4d6978ad08625"
Expand Down Expand Up @@ -20057,6 +20077,13 @@ named-placeholders@^1.1.3:
dependencies:
lru-cache "^7.14.1"

named-placeholders@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/named-placeholders/-/named-placeholders-1.1.6.tgz#c50c6920b43f258f59c16add1e56654f5cc02bb5"
integrity sha512-Tz09sEL2EEuv5fFowm419c1+a/jSMiBjI9gHxVLrVdbUkkNUUfjsVYs9pVZu5oCon/kmRh9TfLEObFtkVxmY0w==
dependencies:
lru.min "^1.1.0"

nan@^2.18.0, nan@^2.19.0, nan@^2.20.0:
version "2.22.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.22.0.tgz#31bc433fc33213c97bad36404bb68063de604de3"
Expand Down