# Better Stack MySQL logging

## Start logging in 2 minutes

What you would like to collect from your MySQL?

- [Collect both logs and metrics from your MySQL instance](#collect-logs-metrics)
- [Collect only logs from your MySQL instance](#collect-only-logs)

## Collect logs & metrics

Collect logs and metrics from your MySQL instance using [Vector](https://vector.dev) and Prometheus [mysqld exporter](https://github.com/prometheus/mysqld_exporter/).

### 1. Set up MySQL exporter

Create a dedicated MySQL user for Vector to securely scrape metrics:

```bash
[label Log in to MySQL]
mysql -u root -p
```

```sql
[label Create a MySQL user]
CREATE USER 'better_stack_metrics'@'localhost' IDENTIFIED BY '$YOUR_PASSWORD';
GRANT PROCESS, REPLICATION CLIENT, SHOW DATABASES ON *.* TO 'better_stack_metrics'@'localhost';
GRANT SELECT ON performance_schema.* TO 'better_stack_metrics'@'localhost';
GRANT SELECT ON mysql.user TO 'better_stack_metrics'@'localhost';
```

[info]
#### Fill in your password

Replace `$YOUR_PASSWORD` with password of your choice.
[/info] 

```sql
[label Flush & exit]
FLUSH PRIVILEGES;
exit;
```

Create `/etc/mysqld_exporter.cnf` config file using MySQL user password you've just set:

```makefile
[label /etc/mysqld_exporter.cnf]
[client]
user=better_stack_metrics
password=$YOUR_PASSWORD
```

### 2. Install MySQL exporter

Install latest mysqld exporter:

```bash
[label Install mysqld exporter]
curl -sSL https://telemetry.betterstack.com/install/mysqld_exporter | sudo -E bash
```

### 3. Run exporter

Start the MySQL exporter:

[code-tabs]
```sh
[label Linux]
systemctl start mysqld_exporter
```
```sg
[label macOS]
launchctl start com.prometheus.mysqld_exporter
```
[/code-tabs]

### 4. Set up Vector

[vector-select-platform]
[vector-platform linux-apt]
Install and configure [Vector](https://vector.dev/) on Ubuntu or Debian:
[/vector-platform]
[vector-platform linux-yum]
Install and configure [Vector](https://vector.dev/) on any Red Hat-based Linux:
[/vector-platform]
[vector-platform macos]
Install and configure [Vector](https://vector.dev/) on macOS:
[/vector-platform]
[vector-platform setup-script-supported]
```bash
[label Set up Vector]
curl -sSL https://telemetry.betterstack.com/setup-vector/mysql_with_metrics/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh
```

[info]
**Curious about what the script does?**

- Installs Vector using official package.
- Configures Vector for your Better Stack source.
- Enables and restarts Vector.

The script will ask for confirmation before making any changes.
[/info]

[/vector-platform]

[vector-platform windows]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/operating-systems/windows/) on Windows using official installer:

```powershell
[label Install Vector]
powershell Invoke-WebRequest https://packages.timber.io/vector/latest/vector-x64.msi -OutFile vector-latest-x64.msi
msiexec /i vector-latest-x64.msi
```


#### Setup

Run in Powershell as administrator to download Vector config:

```powershell
[label Download Vector config using Powershell]
$config_yaml_path = "C:\Program Files\Vector\config\vector.yaml"
$backup_path = "C:\Program Files\Vector\config\vector-backup.yaml"
Move-Item -Path "$config_yaml_path" -Destination "$backup_path"
Invoke-RestMethod -Uri https://telemetry.betterstack.com/vector-yaml/mysql_with_metrics/$SOURCE_TOKEN |
  Add-Content $config_yaml_path
```


#### Run Vector

Run Vector with new configuration:

```powershell
[label Start Vector]
& "C:\Program Files\Vector\bin\vector" --config "C:\Program Files\Vector\config\vector.yaml"
```


[/vector-platform]
[vector-platform linux-other]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/manual/vector-installer/) using official installer:

```sh
[label Install Vector]
curl --proto '=https' --tlsv1.2 -sSf https://sh.vector.dev | sh -s -- -y
```

Create a [Vector service](https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/):

```sh
[label Create a Vector service]
mkdir -p /etc/vector
mkdir -p /var/lib/vector/
cat <<-'SERVICE' >> /etc/systemd/system/vector.service
[Unit]
Description=Vector
Documentation=https://vector.dev
After=network-online.target
Requires=network-online.target

[Service]
User=vector
Group=vector
ExecStart=/root/.vector/bin/vector
ExecReload=/bin/kill -HUP $MAINPID
Restart=no
AmbientCapabilities=CAP_NET_BIND_SERVICE
EnvironmentFile=-/etc/default/vector

[Install]
WantedBy=multi-user.target
SERVICE
```


#### Setup

Download Vector configuration for your source:

```bash
[label Download Vector config]
CONFIG_YAML_PATH="/etc/vector/vector.yaml"
BACKUP_SUFFIX=".bak-$(date +%Y%m%d%H%M%S)"
mv "${CONFIG_YAML_PATH}" "${CONFIG_YAML_PATH}${BACKUP_SUFFIX}"
curl -o "$CONFIG_YAML_PATH" \
  https://telemetry.betterstack.com/vector-yaml/mysql_with_metrics/$SOURCE_TOKEN
```


#### Restart Vector

Restart Vector to reload the configuration:

```sh
[label Restart Vector]
systemctl restart vector
```

[/vector-platform]

 
You should see your logs in [Better Stack → Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank").


## Collect only logs

Collect logs from your MySQL database using Vector.

[vector-select-platform]
[vector-platform linux-apt]
Install and configure [Vector](https://vector.dev/) on Ubuntu or Debian:
[/vector-platform]
[vector-platform linux-yum]
Install and configure [Vector](https://vector.dev/) on any Red Hat-based Linux:
[/vector-platform]
[vector-platform macos]
Install and configure [Vector](https://vector.dev/) on macOS:
[/vector-platform]
[vector-platform setup-script-supported]
```bash
[label Set up Vector]
curl -sSL https://telemetry.betterstack.com/setup-vector/mysql/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh
```

[info]
**Curious about what the script does?**

- Installs Vector using official package.
- Configures Vector for your Better Stack source.
- Enables and restarts Vector.

The script will ask for confirmation before making any changes.
[/info]

[/vector-platform]

[vector-platform windows]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/operating-systems/windows/) on Windows using official installer:

```powershell
[label Install Vector]
powershell Invoke-WebRequest https://packages.timber.io/vector/latest/vector-x64.msi -OutFile vector-latest-x64.msi
msiexec /i vector-latest-x64.msi
```


#### Setup

Run in Powershell as administrator to download Vector config:

```powershell
[label Download Vector config using Powershell]
$config_yaml_path = "C:\Program Files\Vector\config\vector.yaml"
$backup_path = "C:\Program Files\Vector\config\vector-backup.yaml"
Move-Item -Path "$config_yaml_path" -Destination "$backup_path"
Invoke-RestMethod -Uri https://telemetry.betterstack.com/vector-yaml/mysql/$SOURCE_TOKEN |
  Add-Content $config_yaml_path
```


#### Run Vector

Run Vector with new configuration:

```powershell
[label Start Vector]
& "C:\Program Files\Vector\bin\vector" --config "C:\Program Files\Vector\config\vector.yaml"
```


[/vector-platform]
[vector-platform linux-other]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/manual/vector-installer/) using official installer:

```sh
[label Install Vector]
curl --proto '=https' --tlsv1.2 -sSf https://sh.vector.dev | sh -s -- -y
```

Create a [Vector service](https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/):

```sh
[label Create a Vector service]
mkdir -p /etc/vector
mkdir -p /var/lib/vector/
cat <<-'SERVICE' >> /etc/systemd/system/vector.service
[Unit]
Description=Vector
Documentation=https://vector.dev
After=network-online.target
Requires=network-online.target

[Service]
User=vector
Group=vector
ExecStart=/root/.vector/bin/vector
ExecReload=/bin/kill -HUP $MAINPID
Restart=no
AmbientCapabilities=CAP_NET_BIND_SERVICE
EnvironmentFile=-/etc/default/vector

[Install]
WantedBy=multi-user.target
SERVICE
```


#### Setup

Download Vector configuration for your source:

```bash
[label Download Vector config]
CONFIG_YAML_PATH="/etc/vector/vector.yaml"
BACKUP_SUFFIX=".bak-$(date +%Y%m%d%H%M%S)"
mv "${CONFIG_YAML_PATH}" "${CONFIG_YAML_PATH}${BACKUP_SUFFIX}"
curl -o "$CONFIG_YAML_PATH" \
  https://telemetry.betterstack.com/vector-yaml/mysql/$SOURCE_TOKEN
```


#### Restart Vector

Restart Vector to reload the configuration:

```sh
[label Restart Vector]
systemctl restart vector
```

[/vector-platform]

 
You should see your logs in [Better Stack → Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank").

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏


## Additional information

#### Looking for manual instructions to set up Vector?

Find step-by-step instructions in our [dedicated Vector guide](https://betterstack.com/docs/logs/vector/#set-up-vector-manually).