-
Notifications
You must be signed in to change notification settings - Fork 8
/
build_mysql
executable file
·119 lines (100 loc) · 4.13 KB
/
build_mysql
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
if [ -z "$SUDO_USER" ]; then
echo "Please run the script using sudo, e.g.,"
echo ""
echo "sudo ./build_lamp"
exit 1
fi
DATESTRING=$(date +"%y%m%d%H%M")
PORTFOUND=$(type -P port)
if [ "$PORTFOUND" == "" ]; then
echo "port command not found. Please ensure that MacPorts has been installed."
echo "Then log out, log in, and try again."
exit 1
fi
# Remove startup items; these will be replaced.
if [ -L /Library/LaunchDaemons/org.macports.mysql55-server.plist ]; then
rm /Library/LaunchDaemons/org.macports.mysql55-server.plist
fi
if [ -L /Library/LaunchDaemons/org.macports.mysql57-server.plist ]; then
rm /Library/LaunchDaemons/org.macports.mysql57-server.plist
fi
if [ -L /Library/LaunchDaemons/org.macports.mariadb-server.plist ]; then
rm /Library/LaunchDaemons/org.macports.mariadb-server.plist
fi
if [ -L /Library/LaunchDaemons/org.macports.mysql8-server.plist ]; then
rm /Library/LaunchDaemons/org.macports.mysql8-server.plist
fi
# Start with MySQL server. The -N flag loads defaults instead of prompting.
port -N install mysql8-server
if [ -e /opt/local/etc/mysql8/my.cnf ]; then
mv /opt/local/etc/mysql8/my.cnf /opt/local/etc/mysql8/my.cnf.disabled
# Create empty file so as not to anger the macports activation sequence
# https://trac.macports.org/wiki/howto/MySQL#Configurationmy.cnf
touch /opt/local/etc/mysql8/my.cnf
echo "Disabled my.cnf in /opt/local/etc/mysql8/my.cnf; please use /etc/my.cnf"
fi
DATESTRING=$(date +"%y%m%d%H%M")
if [ -e /etc/my.cnf ]; then
mv /etc/my.cnf "/etc/my.cnf.$DATESTRING"
echo "Backed up /etc/my.cnf to /etc/my.cnf.$DATESTRING"
fi
if [ ! -e ./my.cnf ]; then
echo "Could not find my.cnf source."
if [ "${PWD##*/}" != "macports-lamp" ]; then
echo "Please cd into macports-lamp directory before running."
exit 1
fi
else
cp ./my.cnf /etc/my.cnf
fi
# Create initial databases.
/opt/local/lib/mysql8/bin/mysqld --initialize --user=_mysql
port select mysql mysql8
chown -R _mysql:_mysql /opt/local/var/db/mysql8/
chown -R _mysql:_mysql /opt/local/var/run/mysql8/
chown -R _mysql:_mysql /opt/local/var/log/mysql8/
# Port install may have loaded already.
port unload mysql8-server > /dev/null 2>&1
# Start MySQL.
port -d load mysql8-server
echo
echo
echo "Please enter the root password you'd like to use for MySQL:"
read -rs ROOTPW
echo
echo "Your Mac sudo password may be needed"
# Make it possible to view MySQL error log without sudo
sudo chgrp -R staff /opt/local/var/log/mysql8
# Parse autogenerated password from startup log.
MYP=$(sudo grep 'temporary password' /opt/local/var/log/mysql8/mysql.log | tail -1 | awk '{ print $13 }')
/opt/local/lib/mysql8/bin/mysql --connect-expired-password -uroot -p"${MYP}" -e "ALTER USER root@localhost IDENTIFIED BY '${ROOTPW}'"
/opt/local/lib/mysql8/bin/mysql -uroot -p"${ROOTPW}" -e "DROP DATABASE IF EXISTS test;
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;"
# Warn on old path to mariadb binaries.
grep -qF '^PATH=$PATH:/opt/local/lib/mariadb/bin' ~/.bash_profile || echo; echo 'WARNING: you should comment out the following deprecated line in your .bash_profile: PATH=$PATH:/opt/local/lib/mariadb/bin'; echo
echo "MySQL 8 will launch automatically at startup. If you don't want this type:"
echo "sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql8-server.plist"
echo
echo "And if you change your mind and want to load it at startup again, type:"
echo "sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql8-server.plist"
echo
echo "-----"
echo "If you cannot get into MySQL you can reset the root password by"
echo "starting it manually:"
echo
echo "sudo -u _mysql /opt/local/lib/mysql8/bin/mysqld --user=_mysql --skip-grant-tables"
echo
echo "Then type mysql at the command line and set the root password:"
echo
echo "USE mysql;"
echo "UPDATE user SET password=PASSWORD('foo') WHERE User='root';"
echo "FLUSH PRIVILEGES;"
echo "-----"
echo
echo "MySQL connections are available through the following local socket:"
echo "/opt/local/var/run/mysql8/mysqld.sock"
echo