The MySQL connector is used to query an external MySQL database.
MySQL server installation.
Hopefully you have installed mysql server on your machine. To enable mysql properties on Presto server, you must create a file “mysql.properties” in “etc/catalog” directory. Issue the following command to create a mysql.properties file.
$ cd etc $ cd catalog $ vi mysql.properties connector.name = mysql connection-url = jdbc:mysql://localhost:3306 connection-user = root connection-password = pwd
Save the file and quit the terminal. In the above file, you must enter your mysql password in connection-password field.
Open MySQL server and create a database using the following command.
create database tutorials
Now you have created “tutorials” database in the server. To enable database type, use the command “use tutorials” in the query window.
Let’s create a simple table on “tutorials” database.
create table author(auth_id int not null, auth_name varchar(50),topic varchar(100))
After creating a table, insert three records using the following query.
insert into author values(1,'Doug Cutting','Hadoop') insert into author values(2,’James Gosling','java') insert into author values(3,'Dennis Ritchie’,'C')
To retrieve all the records, type the following query.
select * from author
auth_id auth_name topic 1 Doug Cutting Hadoop 2 James Gosling java 3 Dennis Ritchie C
As of now, you have queried data using MySQL server. Let’s connect Mysql storage plugin to Presto server.
Type the following command to connect MySql plugin on Presto CLI.
./presto --server localhost:8080 --catalog mysql --schema tutorials
You will receive the following response.
presto:tutorials>
Here “tutorials” refers to schema in mysql server.
To list out all the schemas in mysql, type the following query in Presto server.
presto:tutorials> show schemas from mysql;
Schema -------------------- information_schema performance_schema sys tutorials
From this result, we can conclude the first three schemas as predefined and the last one as created by yourself.
Following query lists out all the tables in tutorials schema.
presto:tutorials> show tables from mysql.tutorials;
Table -------- author
We have created only one table in this schema. If you have created multiple tables, it will list out all the tables.
To describe the table fields, type the following query.
presto:tutorials> describe mysql.tutorials.author;
Column | Type | Comment -----------+--------------+--------- auth_id | integer | auth_name | varchar(50) | topic | varchar(100) |
presto:tutorials> show columns from mysql.tutorials.author;
Column | Type | Comment -----------+--------------+--------- auth_id | integer | auth_name | varchar(50) | topic | varchar(100) |
To fetch all the records from mysql table, issue the following query.
presto:tutorials> select * from mysql.tutorials.author;
auth_id | auth_name | topic ---------+----------------+-------- 1 | Doug Cutting | Hadoop 2 | James Gosling | java 3 | Dennis Ritchie | C
From this result, you can retrieve mysql server records in Presto.
Mysql connector doesn’t support create table query but you can create a table using as command.
presto:tutorials> create table mysql.tutorials.sample as select * from mysql.tutorials.author;
CREATE TABLE: 3 rows
You can’t insert rows directly because this connector has some limitations. It cannot support the following queries −
To view the records in the newly created table, type the following query.
presto:tutorials> select * from mysql.tutorials.sample;
auth_id | auth_name | topic ---------+----------------+-------- 1 | Doug Cutting | Hadoop 2 | James Gosling | java 3 | Dennis Ritchie | C