MySQL Database Server

Installs and secures MySQL 8.0 database server

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 10 months ago
Size 2 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#cloud-config
package_update: true
package_upgrade: true

packages:
  - mysql-server
  - mysql-client

write_files:
  - path: /etc/mysql/mysql.conf.d/mysqld_custom.cnf
    content: |
      [mysqld]
      bind-address = 127.0.0.1
      max_connections = 200
      innodb_buffer_pool_size = 256M
      query_cache_size = 16M
      tmp_table_size = 32M
      max_heap_table_size = 32M
      
  - path: /tmp/mysql_secure_installation.sql
    content: |
      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'StrongPassword123!';
      DELETE FROM mysql.user WHERE User='';
      DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
      DROP DATABASE IF EXISTS test;
      DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
      FLUSH PRIVILEGES;

runcmd:
  - systemctl enable mysql
  - systemctl start mysql
  - mysql < /tmp/mysql_secure_installation.sql
  - rm /tmp/mysql_secure_installation.sql
  - systemctl restart mysql
  - ufw allow 3306/tcp

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml