-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-database.sh
80 lines (65 loc) · 2.58 KB
/
setup-database.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# Yum Update
yum update -y
# Adds the MySQL Repo
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -P /tmp/dploy
rpm -ivh /tmp/dploy/mysql80-community-release-el7-3.noarch.rpm
# Install Requirements
yum install -y expect mysql-server
# Extracts Params from OvfEnv Variables
LAB_DATABASE_IP=`vmtoolsd --cmd "info-get guestinfo.ovfEnv" | grep 'lab.db.ip' | awk -F\" '{print $4}'`
LAB_DATABASE_FQDN=`vmtoolsd --cmd "info-get guestinfo.ovfEnv" | grep 'lab.db.fqdn' | awk -F\" '{print $4}'`
LAB_DATABASE_PASSWD=`vmtoolsd --cmd "info-get guestinfo.ovfEnv" | grep 'lab.db.password' | awk -F\" '{print $4}'`
LAB_DATABASE_NAME=`vmtoolsd --cmd "info-get guestinfo.ovfEnv" | grep 'lab.db.name' | awk -F\" '{print $4}'`
LAB_DATABASE_USER=`vmtoolsd --cmd "info-get guestinfo.ovfEnv" | grep 'lab.db.user' | awk -F\" '{print $4}'`
echo "$LAB_DATABASE_IP - $LAB_DATABASE_FQDN - $LAB_DATABASE_PASSWD - $LAB_DATABASE_NAME" >> /tmp/dploy.log
# First Start
systemctl start mysqld
sleep 5
MYSQL_ROOT_TMP_PASS=$(grep "temporary password" /var/log/mysqld.log | awk '{print $13}')
echo "TMP Pass: $MYSQL_ROOT_TMP_PASS" >> /tmp/dploy.log
systemctl restart mysqld
# Runs the Secure installation using 'expect
expect -f - <<-EOF
set timeout 10
spawn mysql_secure_installation
expect "Enter password for user root*"
send "$MYSQL_ROOT_TMP_PASS\r"
expect "New password*"
send "Telefonica.2020\r"
expect "Re-enter new password*"
send "Telefonica.2020\r"
expect "Change the password for root ? ((Press y|Y for Yes, any other key for No)*"
send "n\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No)*"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No)*"
send "y\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No)*"
send "y\r"
expect "Reload privilege tables now?*"
send "y\r"
expect eof
EOF
cat << EOF > /root/.my.cnf
[mysql]
user="root"
password="Telefonica.2020"
[mysqld]
bind-address = 127.0.0.1
bind-address = $LAB_DATABASE_IP
EOF
# Creates the SQL statetemt to create the user
cat << EOF > /tmp/dploy/create-user.sql
CREATE USER '$LAB_DATABASE_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$LAB_DATABASE_PASSWD';
GRANT ALL PRIVILEGES ON *.* TO '$LAB_DATABASE_USER'@'%';
FLUSH PRIVLEGES;
EOF
# Creates the Database
mysql -uroot < /tmp/dploy/create-db.sql
# Creates the User
mysql -uroot < /tmp/dploy/create-user.sql
# Opens ports
firewall-cmd --permanent --zone=trusted --add-source=10.0.2.0/24
firewall-cmd --permanent --zone=trusted --add-port=3306/tcp
firewall-cmd --reload