Before you begin
Make sure you have:- An Algolia account. Create one for free if you don’t already have one.
- A MySQL host that Algolia can reach.
Use a hosted MySQL server or configure an SSH tunnel to your server.
A server available only at
127.0.0.1orlocalhostisn’t reachable from the hosted Algolia connector. - Your MySQL host’s port number.
The default is
3306but your provider may use a different port. - A MySQL database. Create or choose a database for the sample data and note its name. For more information, see Creating and selecting a database.
- A MySQL username and password.
The user must have
CREATE,DROP,INSERT, andSELECTaccess to the target database. For more information, see GRANT Statement.
Set up the MySQL connector
Create a source, destination, transformation, and synchronization task for the sample product dataset.- The source tells the connector which MySQL database to read.
- The destination tells the connector which Algolia index to write to.
- The transformation converts MySQL JSON values and enriches the records.
- The task controls which table to index and when the connector runs.
1
Download the sample SQL file
Command line
2
Import the sample data into MySQL
Run the script:Replace
Command line
MYSQL_HOST, MYSQL_PORT, MYSQL_USER and MYSQL_DATABASE with your MySQL connection details.When prompted, enter the password for MYSQL_USER.For more information,
see Executing SQL statements from a text file.3
Verify the import
Run:When prompted, enter the password for
Command line
MYSQL_USER.The SQL query should return 1000 records.4
Choose the MySQL connector
Go to the Algolia dashboard and select your Algolia application.
Open the Connectors page.Find MySQL, then select Connect.
5
Configure the MySQL source
Under Configure your data source, select Create a new source.Enter the connection details for the database table that contains the sample data:
Under SSH Tunnel Method, select No Tunnel if Algolia can connect directly to your database.
Otherwise, configure the authentication for your SSH tunnel.
6
Create the MySQL source
In Connector name, enter
Quickstart products MySQL source, then select Create source.7
Transform the records
This transformation adds a Select Save.
price_range attribute to each record.
After the connector indexes your records, you can display price_range or configure it as a facet.Select Transform using the code editor and replace the placeholder Transformation code with this function:JavaScript
8
Choose the destination index
Under Configure your destination, select Create a new destination.Under Search, enter
quickstart-products as the index name.9
Create the destination
Under Index credentials, select Create one for me.
To use an existing API key, choose one with the
addObject, deleteIndex, and editSettings
ACLs.In Name, enter MySQL quickstart products destination, then select Create destination.10
Create and run an on-demand synchronization task
Under Configure your task, select On demand.Select the
apparel table and keep all its columns selected.
Confirm that the connector maps the table’s objectID primary key to each record’s Algolia objectID.Select Create task, then Run and wait for the task to finish.11
Verify the indexed records
When the task completes, open the
quickstart-products index in the Algolia dashboard.
The index contains 1,000 product records with attributes such as title, description, product_type,
price, price_range, and showcase_image.You can use this index as the data source for Build your first search experience.
Before you build the UI,
configure
product_type as an attribute for faceting
in the quickstart-products index.Index your own MySQL data
Use a dedicated read-only MySQL account with access only to the tables you want to index, and restrict network access to Algolia’s connector IP addresses.Set up a dedicated MySQL connector user
Connect to MySQL with an account that can create users and grant privileges. Run the following statements in the MySQL client or your hosting provider’s SQL console. Create a dedicated connector user and grant itSELECT access to the table you want to index.
For example:
SQL
MYSQL_USER_PASSWORD and MYSQL_DATABASE.TABLE_NAME with your connector user’s password, database, and table name.
If the user already exists, run only the GRANT statement.
Allow the Algolia connector to reach your MySQL server
The% host permits remote connections.
Configure the connector for your data
- Follow the steps to configure the MySQL source and create a synchronization task: replace the sample connection details and
appareltable with your own database details and tables. - Use the dedicated connector user you created.
- Review the transformation and destination index to make sure they’re appropriate for your data.
Secure the connection
Configure connection security (SSL modes) in your MySQL source. SSL encrypts the connection to your MySQL server and can verify the server’s identity. An SSH tunnel lets the connector reach a database that isn’t directly accessible from Algolia. Depending on your setup, you can use SSL, an SSH tunnel, or both.Configure SSL
Choose an SSL mode that matches your MySQL server’s TLS configuration:- Preferred. The connector uses SSL if the MySQL server supports it. Otherwise, it connects without SSL.
- Required requires an encrypted connection but doesn’t verify the server certificate.
- Verify CA requires encryption and verifies the server certificate against a trusted certificate authority. Add the CA certificate supplied by your database provider to CA certificate.
- Verify Identity verifies that the certificate identifies the host. Add the provider’s CA certificate.
Connect through an SSH tunnel
If Algolia can’t connect directly to your MySQL database, connect through an SSH host. Select an authentication method:- SSH Key Authentication
- Password Authentication
To authenticate with an RSA private key:
-
Generate an RSA key pair in PEM format:
Command line
-
Add
algolia_mysql_rsa.pubto the SSH user’s authorized keys on the bastion host. - In the MySQL source configuration, select SSH Key Authentication under SSH Tunnel Method.
- Enter the SSH Tunnel Jump Server Host, SSH port, and SSH Login Username.
-
Add the contents of
algolia_mysql_rsato SSH Private Key.
MySQL data types
The connector maps MySQL values to JSON-compatible Algolia attributes. In particular:DECIMALvalues become numbers.TINYINT(1)values become booleans by default.- Native MySQL
JSONvalues become serialized JSON strings. TEXTandLONGTEXTcolumns remain strings, including columns that contain JSON-encoded data.
Transformations
Use transformations to add computed attributes or change values before the connector indexes your records. To use a transformed attribute for search, faceting, or ranking, update the relevant index settings, such assearchableAttributes, attributesForFaceting, or customRanking.
Test transformations with repeated task runs.
Transformations that read from and write to the same attribute,
or extract a value and then delete the source attribute,
can produce different results when the task runs again.
Synchronization schedule
After you create a task, you can edit it in the Algolia dashboard to choose when the connector runs:- On demand. Run the connector manually.
- Scheduled. Select a predefined schedule or enter a custom cron expression.