You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/integrations/data-sources/mysql.md
+142-2Lines changed: 142 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,146 @@ hide_title: true
6
6
description: 'Page describing MySQL integration'
7
7
---
8
8
9
-
import MySQL from '@site/docs/integrations/data-ingestion/dbms/mysql/index.md';
9
+
import CloudNotSupportedBadge from '@theme/badges/CloudNotSupportedBadge';
10
+
import ExperimentalBadge from '@theme/badges/ExperimentalBadge';
11
+
12
+
# Integrating MySQL with ClickHouse
13
+
14
+
This page covers using the `MySQL` table engine, for reading from a MySQL table.
15
+
16
+
:::note
17
+
For ClickHouse Cloud, you can also use the [MySQL ClickPipe](/integrations/clickpipes/mysql) (currently in public beta) to easily move data from your MySQL tables to ClickHouse.
18
+
:::
19
+
20
+
## Connecting ClickHouse to MySQL using the MySQL Table Engine {#connecting-clickhouse-to-mysql-using-the-mysql-table-engine}
21
+
22
+
The `MySQL` table engine allows you to connect ClickHouse to MySQL. **SELECT** and **INSERT** statements can be made in either ClickHouse or in the MySQL table. This article illustrates the basic methods of how to use the `MySQL` table engine.
23
+
24
+
### 1. Configure MySQL {#1-configure-mysql}
25
+
26
+
1. Create a database in MySQL:
27
+
```sql
28
+
CREATEDATABASEdb1;
29
+
```
30
+
31
+
2. Create a table:
32
+
```sql
33
+
CREATETABLEdb1.table1 (
34
+
id INT,
35
+
column1 VARCHAR(255)
36
+
);
37
+
```
38
+
39
+
3. Insert sample rows:
40
+
```sql
41
+
INSERT INTOdb1.table1
42
+
(id, column1)
43
+
VALUES
44
+
(1, 'abc'),
45
+
(2, 'def'),
46
+
(3, 'ghi');
47
+
```
48
+
49
+
4. Create a user to connect from ClickHouse:
50
+
```sql
51
+
CREATEUSER 'mysql_clickhouse'@'%' IDENTIFIED BY 'Password123!';
52
+
```
53
+
54
+
5. Grant privileges as needed. (For demonstration purposes, the `mysql_clickhouse` user is granted admin privileges.)
55
+
```sql
56
+
GRANT ALL PRIVILEGES ON*.* TO 'mysql_clickhouse'@'%';
57
+
```
58
+
59
+
:::note
60
+
If you are using this feature in ClickHouse Cloud, you may need the to allow the ClickHouse Cloud IP addresses to access your MySQL instance.
61
+
Check the ClickHouse [Cloud Endpoints API](//cloud/get-started/query-endpoints.md) for egress traffic details.
62
+
:::
63
+
64
+
### 2. Define a Table in ClickHouse {#2-define-a-table-in-clickhouse}
65
+
66
+
1. Now let's create a ClickHouse table that uses the `MySQL` table engine:
|user |username to connect to mysql|mysql_clickhouse |
83
+
|password |password to connect to mysql|Password123! |
84
+
85
+
:::note
86
+
View the [MySQL table engine](/engines/table-engines/integrations/mysql.md) doc page for a complete list of parameters.
87
+
:::
88
+
89
+
### 3. Test the Integration {#3-test-the-integration}
90
+
91
+
1. In MySQL, insert a sample row:
92
+
```sql
93
+
INSERT INTOdb1.table1
94
+
(id, column1)
95
+
VALUES
96
+
(4, 'jkl');
97
+
```
98
+
99
+
2. Notice the existing rows from the MySQL table are in the ClickHouse table, along with the new row you just added:
100
+
```sql
101
+
SELECT
102
+
id,
103
+
column1
104
+
FROM mysql_table1
105
+
```
106
+
107
+
You should see 4 rows:
108
+
```response
109
+
Query id: 6d590083-841e-4e95-8715-ef37d3e95197
110
+
111
+
┌─id─┬─column1─┐
112
+
│ 1 │ abc │
113
+
│ 2 │ def │
114
+
│ 3 │ ghi │
115
+
│ 4 │ jkl │
116
+
└────┴─────────┘
117
+
118
+
4 rows in set. Elapsed: 0.044 sec.
119
+
```
120
+
121
+
3. Let's add a row to the ClickHouse table:
122
+
```sql
123
+
INSERT INTO mysql_table1
124
+
(id, column1)
125
+
VALUES
126
+
(5,'mno')
127
+
```
128
+
129
+
4. Notice the new row appears in MySQL:
130
+
```bash
131
+
mysql>selectid,column1 from db1.table1;
132
+
```
133
+
134
+
You should see the new row:
135
+
```response
136
+
+------+---------+
137
+
| id | column1 |
138
+
+------+---------+
139
+
| 1 | abc |
140
+
| 2 | def |
141
+
| 3 | ghi |
142
+
| 4 | jkl |
143
+
| 5 | mno |
144
+
+------+---------+
145
+
5 rows inset (0.01 sec)
146
+
```
147
+
148
+
### Summary {#summary}
149
+
150
+
The `MySQL` table engine allows you to connect ClickHouse to MySQL to exchange data back and forth. For more details, be sure to check out the documentation page for the [MySQL table engine](/sql-reference/table-functions/mysql.md).
0 commit comments