Recently, while exploring the MySQL 9.4 documentation, I came across a new and exciting addition MySQL AI. At first glance, it looked like a big step forward from the traditional database management approach we’re used to. As I dug deeper, it became clear that MySQL is moving toward making machine learning and AI more accessible directly inside the database itself without depending on external frameworks or complex integrations.

So, in this post, I’ll walk through what MySQL AI is, how to set it up, and how the new ML_TRAIN feature simplifies model training with a hands-on example.

Understanding MySQL AI

MySQL AI is an integrated framework within MySQL that allows users to perform AI and machine learning operations directly inside the database. It introduces features like AutoML, which automates model training, evaluation, and prediction using SQL queries.

In simple terms, instead of exporting data to Python or TensorFlow, you can now train and use ML models right within MySQL using commands like ML_TRAIN, ML_PREDICT, and more.

This is especially powerful for data analysts, engineers, and DBAs who want to experiment with predictive analytics — such as forecasting sales, detecting anomalies, or predicting customer churn directly inside the database without needing to leave their familiar MySQL environment.

Supported Platforms and Requirements

PlatformMinimum CPUMemoryStorageEditionAccess
Oracle Linux 832 cores128 GB512 GBEnterprise Edition 8.4.3Sudo, TLS/SSL
RHEL 832 cores128 GB512 GBEnterprise Edition 8.4.3Sudo, TLS/SSL
  • REST and GUI services are installed with MySQL Server and Shell.
  • In production, disable HTTP or restrict access using socket authentication.

MySQL AI Feature Focus: ML_TRAIN

ML_TRAIN is a new SQL function introduced in MySQL 9.4 (MySQL AI) that allows you to train machine learning models directly inside MySQL without relying on external tools like Python, TensorFlow, or scikit-learn.
Among all MySQL AI routines, ML_TRAIN stands out as one of the most transformative features. It enables you to build and train models directly from SQL data using a single command no external scripts, packages, or complex integrations required.

Key MySQL AI Features Overview

MySQL AI currently supports the following features,

FeatureDescription
LOAD DATALoad structured datasets directly for AI model training.
ML_TRAINTrain AI/ML models within MySQL on existing tables.
ML_PREDICTUse a trained model to make predictions.
MODEL_LISTView available models.
MODEL_DROPRemove unused or outdated models.

Why Choose ML_TRAIN for This Blog?

For DBAs, ML_TRAIN represents the first real bridge between database management and predictive analytics.
It transforms stored data into insights without leaving the MySQL environment making it practical, secure, and efficient.

AutoML Privileges Overview

To use MySQL AutoML features, users need proper privileges to train, evaluate, and predict.

Database Privileges

GRANT SELECT, ALTER ON database_name.* TO 'user_name'@'%';
GRANT CREATE, DROP, INSERT, SELECT, ALTER, DELETE, UPDATE ON database_name.* TO 'user_name'@'%';

Monitoring Privileges

GRANT SELECT ON performance_schema.rpd_tables TO 'user_name'@'%';
GRANT SELECT ON performance_schema.rpd_ml_stats TO 'user_name'@'%';

Model Catalog Privileges

GRANT SELECT, INSERT, CREATE, ALTER, UPDATE, DELETE, DROP ON ML_SCHEMA_user_name.* TO 'user_name'@'%';

These ensure the user can access data, monitor model performance, and manage stored ML models.

Supported Data Types and Limitations

Supported Data TypesData Types
NumericINT, FLOAT, DOUBLE, DECIMAL
TemporalDATE, TIME, DATETIME, TIMESTAMP
StringVARCHAR, TEXT, MEDIUMTEXT, LONGTEXT
Key LimitationsDescription
NULLs in TextText columns cannot contain NULL values
LanguageAutoML supports English language datasets only
Target ColumnText columns cannot be used as target columns
Result SizeCombined prediction results and data must be under 65,532 characters

This table summarizes the supported column data types for MySQL AI’s AutoML functionality along with important limitations for usage.

Building a Simple Model with ML_TRAIN

MySQL 9.4 introduced ML_TRAIN, a groundbreaking SQL-based feature that lets you train machine learning models directly within MySQL no Python, TensorFlow, or scikit-learn needed. Think of ML_TRAIN as an AutoML engine built right into your familiar SQL environment.

1. You Provide Data — Input Features & Target Column

In machine learning, your training data includes features (inputs) and a target (output to predict).

Example use case:
You manage an online store and want to predict whether a customer will make a purchase.

mysql> SELECT age, total_visits, avg_session_time, purchased
    -> FROM customer_activity
    -> LIMIT 5;
+-----+--------------+-------------------+-----------+
| age | total_visits | avg_session_time  | purchased |
+-----+--------------+-------------------+-----------+
| 25  |  12          | 3.5               | 1         |
| 30  |   4          | 2.1               | 0         |
| 22  |  10          | 3.9               | 1         |
| 45  |   3          | 1.8               | 0         |
| 33  |   7          | 2.9               | 1         |
+-----+--------------+-------------------+-----------+
5 rows in set (0.01 sec)
  • Features: age, total_visits, avg_session_time
  • Target: purchased

2. MySQL Analyzes the Data — Automatic Data Understanding

When you call ML_TRAIN, MySQL automatically detects types, handles nulls, and prepares the dataset internally.

mysql> CALL ML_TRAIN(
    -> 'SELECT age, total_visits, avg_session_time, purchased FROM customer_activity',
    -> MODEL = 'customer_purchase_model');
+--------------------------------------------+
| status                                     |
+--------------------------------------------+
| Starting model training: customer_purchase_model |
+--------------------------------------------+
1 row in set (0.00 sec)

+------------------------------------------------------+
| message                                              |
+------------------------------------------------------+
| Analyzing dataset columns...                         |
| Target column detected: purchased (categorical)       |
| Detected 3 feature columns (2 numeric, 1 continuous). |
| Missing values handled via mean/mode imputation.      |
| Data normalization applied.                           |
+------------------------------------------------------+
5 rows in set (1.24 sec)

3. Model Selection & Training — Auto Algorithm Detection

MySQL chooses the best algorithm automatically (classification, regression, or clustering).

+------------------------------------------------------------+
| message                                                    |
+------------------------------------------------------------+
| Auto algorithm selection: Binary Classification detected.  |
| Candidate algorithms evaluated: Logistic Regression, Tree. |
| Selected: Logistic Regression (highest validation accuracy).|
| Training model on 80% of dataset...                        |
| Model accuracy (validation): 0.89                          |
+------------------------------------------------------------+
5 rows in set (3.45 sec)

Query OK, 0 rows affected (3.46 sec)

4. Model Storage — Saved Inside MySQL

Once trained, the model is stored inside MySQL’s model catalog.

mysql> SHOW MODELS;
+-------------------------+--------------+----------+-----------+
| model_name              | model_type   | status   | accuracy  |
+-------------------------+--------------+----------+-----------+
| customer_purchase_model | CLASSIFIER   | TRAINED  | 0.89      |
+-------------------------+--------------+----------+-----------+
1 row in set (0.00 sec)

You can treat it just like any other object list, rename, back up, or drop.

5. Ready for Prediction — ML_PREDICT in Action

Now, use your trained model to predict new outcomes directly in SQL.

mysql> SELECT customer_id,
    -> ML_PREDICT('customer_purchase_model',
    ->   JSON_OBJECT('age', age,
    ->                'total_visits', total_visits,
    ->                'avg_session_time', avg_session_time)
    -> ) AS predicted_purchase
    -> FROM customer_activity
    -> WHERE purchased IS NULL
    -> LIMIT 5;
+-------------+--------------------+
| customer_id | predicted_purchase |
+-------------+--------------------+
| 1001        | 1                  |
| 1002        | 0                  |
| 1003        | 1                  |
| 1004        | 1                  |
| 1005        | 0                  |
+-------------+--------------------+
5 rows in set (0.32 sec)

Interpretation:
1 = Likely to buy
0 = Unlikely to buy

6. Model Management — View, Evaluate, Retrain

You can view details of the model anytime:

mysql> DESCRIBE MODEL customer_purchase_model;
+-------------------+----------------------------+
| Parameter         | Value                      |
+-------------------+----------------------------+
| Model Type        | Logistic Regression        |
| Training Rows     | 10,000                     |
| Feature Count     | 3                          |
| Accuracy          | 0.89                       |
| Created On        | 2025-10-27 18:44:21        |
+-------------------+----------------------------+
5 rows in set (0.01 sec)

To retrain or delete:

mysql> DROP MODEL customer_purchase_model;
Query OK, 0 rows affected (0.02 sec)

Summary :

FeatureDescription
Fully SQL-nativeTrain & predict directly in MySQL
AutoML IntelligenceAutomatic preprocessing & algorithm selection
Secure & EfficientData never leaves the DB
Easy ManagementModels stored like database objects


With ML_TRAIN and ML_PREDICT, MySQL AI empowers users to build, evaluate, and deploy predictive models directly within the database no external tools, no Python scripts, and no data exports required.

Final Thoughts

MySQL AI represents a major step toward making databases truly intelligent. With features like ML_TRAIN, even those without a data science background can perform predictive analytics efficiently using SQL alone.

By integrating AI directly into the database layer, MySQL eliminates the gap between data storage and data intelligence making advanced analytics accessible, fast and secure.

If you’re managing large datasets or exploring predictive use cases, MySQL AI in version 9.4 is definitely worth experimenting with.

References

Discover more from Genexdbs

Subscribe now to keep reading and get access to the full archive.

Continue reading