Skip to content

Commit 4d0f0ea

Browse files
committed
fix: Mysql 5.6 drop user separate sql file
1 parent f7e1171 commit 4d0f0ea

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

recipes/newrelic/infrastructure/ohi/mysql/debian.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,17 @@ install:
179179
fi
180180
181181
# Create new user with needed permissions, delete any previous user if exists
182-
if [ -f /tmp/sql-create-user.sql ]; then
183-
sudo rm -f /tmp/sql-create-user.sql
184-
fi
185-
sudo tee -a /tmp/sql-create-user.sql > /dev/null <<"EOT"
186-
GRANT USAGE ON *.* to 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
182+
sudo tee /tmp/sql-drop-user.sql > /dev/null <<"EOT"
187183
DROP USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost';
188-
GRANT REPLICATION CLIENT, SELECT ON *.* TO 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
189184
EOT
185+
186+
sudo tee /tmp/sql-create-user.sql > /dev/null <<"EOT"
187+
CREATE USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
188+
GRANT REPLICATION CLIENT ON *.* TO 'newrelic'@'localhost';
189+
GRANT SELECT ON *.* TO 'newrelic'@'localhost';
190+
EOT
191+
192+
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-drop-user.sql
190193
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-create-user.sql
191194
sudo sed -i 's/NEW_RELIC_MYSQL_PASSWORD/'$NEW_RELIC_MYSQL_PASSWORD'/g' /tmp/sql-create-user.sql
192195
@@ -203,6 +206,7 @@ install:
203206
read -r USERNAME
204207
USERNAME=${USERNAME:-root}
205208
209+
sudo mysql -u $USERNAME --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
206210
EXEC_OUTPUT=$(eval sudo mysql -u $USERNAME --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
207211
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
208212
SQL_OUTPUT_ERROR=$(echo -n $EXEC_OUTPUT | grep "ERROR" | wc -l)
@@ -213,15 +217,18 @@ install:
213217
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPerhaps the MySql root DB password is incorrect.\nPlease either re-run the installation with the correct root DB password, or follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/\n\n" >&2
214218
sudo cat /tmp/sql-create-user.sql
215219
sudo rm -f /tmp/sql-create-user.sql
220+
sudo rm -f /tmp/sql-drop-user.sql
216221
printf "\n"
217222
echo -e "detail:"$EXEC_OUTPUT
218223
printf "\n"
219224
exit 131
220225
fi
221226
else
222227
if [[ -z "$NEW_RELIC_MYSQL_ROOT_PASSWORD" ]]; then
228+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-drop-user.sql &> /dev/null ||:
223229
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-create-user.sql 2>&1)
224230
else
231+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
225232
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
226233
fi
227234
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
@@ -230,13 +237,15 @@ install:
230237
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPlease follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/ \n\n" >&2
231238
sudo cat /tmp/sql-create-user.sql
232239
sudo rm -f /tmp/sql-create-user.sql
240+
sudo rm -f /tmp/sql-drop-user.sql
233241
printf "\n"
234242
echo -e "detail:"$EXEC_OUTPUT
235243
printf "\n"
236244
exit 131
237245
fi
238246
fi
239247
sudo rm -f /tmp/sql-create-user.sql
248+
sudo rm -f /tmp/sql-drop-user.sql
240249
241250
# Install the integration
242251
sudo mkdir -p "/etc/newrelic-infra/integrations.d"

recipes/newrelic/infrastructure/ohi/mysql/rhel.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,17 @@ install:
168168
fi
169169
170170
# Create new user with needed permissions, delete any previous user if exists
171-
if [ -f /tmp/sql-create-user.sql ]; then
172-
sudo rm -f /tmp/sql-create-user.sql
173-
fi
174-
sudo tee -a /tmp/sql-create-user.sql > /dev/null <<"EOT"
175-
GRANT USAGE ON *.* to 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
171+
sudo tee /tmp/sql-drop-user.sql > /dev/null <<"EOT"
176172
DROP USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost';
177-
GRANT REPLICATION CLIENT, SELECT ON *.* TO 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
178173
EOT
174+
175+
sudo tee /tmp/sql-create-user.sql > /dev/null <<"EOT"
176+
CREATE USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
177+
GRANT REPLICATION CLIENT ON *.* TO 'newrelic'@'localhost';
178+
GRANT SELECT ON *.* TO 'newrelic'@'localhost';
179+
EOT
180+
181+
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-drop-user.sql
179182
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-create-user.sql
180183
sudo sed -i 's/NEW_RELIC_MYSQL_PASSWORD/'$NEW_RELIC_MYSQL_PASSWORD'/g' /tmp/sql-create-user.sql
181184
@@ -192,6 +195,7 @@ install:
192195
read -r USERNAME
193196
USERNAME=${USERNAME:-root}
194197
198+
sudo mysql -u $USERNAME --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
195199
EXEC_OUTPUT=$(eval sudo mysql -u $USERNAME --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
196200
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
197201
SQL_OUTPUT_ERROR=$(echo -n $EXEC_OUTPUT | grep "ERROR" | wc -l)
@@ -203,15 +207,18 @@ install:
203207
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPerhaps the MySql root DB password is incorrect.\nPlease either re-run the installation with the correct root DB password, or follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/ \n\n" >&2
204208
sudo cat /tmp/sql-create-user.sql
205209
sudo rm -f /tmp/sql-create-user.sql
210+
sudo rm -f /tmp/sql-drop-user.sql
206211
printf "\n"
207212
echo -e "detail:"$EXEC_OUTPUT
208213
printf "\n"
209214
exit 131
210215
fi
211216
else
212217
if [[ -z "$NEW_RELIC_MYSQL_ROOT_PASSWORD" ]]; then
218+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-drop-user.sql &> /dev/null ||:
213219
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-create-user.sql 2>&1)
214220
else
221+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
215222
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
216223
fi
217224
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
@@ -220,13 +227,15 @@ install:
220227
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPlease follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/\n\n" >&2
221228
sudo cat /tmp/sql-create-user.sql
222229
sudo rm -f /tmp/sql-create-user.sql
230+
sudo rm -f /tmp/sql-drop-user.sql
223231
printf "\n"
224232
echo -e "detail:"$EXEC_OUTPUT
225233
printf "\n"
226234
exit 131
227235
fi
228236
fi
229237
sudo rm -f /tmp/sql-create-user.sql
238+
sudo rm -f /tmp/sql-drop-user.sql
230239
231240
# Install the integration
232241
sudo mkdir -p "/etc/newrelic-infra/integrations.d"

recipes/newrelic/infrastructure/ohi/mysql/suse.yml

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,25 @@ install:
164164
fi
165165
166166
# Create new user with needed permissions, delete any previous user if exists
167-
if [ -f /tmp/sql-create-user.sql ]; then
168-
sudo rm -f /tmp/sql-create-user.sql
169-
fi
170-
171-
sudo tee -a /tmp/sql-create-user.sql > /dev/null <<"EOT"
172-
GRANT USAGE ON *.* to 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
167+
sudo tee /tmp/sql-drop-user.sql > /dev/null <<"EOT"
173168
DROP USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost';
174-
GRANT REPLICATION CLIENT, SELECT ON *.* TO 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
175169
EOT
176170
177-
sudo sed -i 's/NR_CLI_DB_USERNAME/'$NR_CLI_DB_USERNAME'/g' /tmp/sql-create-user.sql
178-
sudo sed -i 's/NR_CLI_DB_PASSWORD/'$NR_CLI_DB_PASSWORD'/g' /tmp/sql-create-user.sql
179-
if [ $MYSQL_ROOT_PASSWORD -gt 0 ] ; then
180-
181-
printf "MySql credential with permission to create user, and grant select/replication permission is required, please enter it below when prompted\n"
171+
sudo tee /tmp/sql-create-user.sql > /dev/null <<"EOT"
172+
CREATE USER 'NEW_RELIC_MYSQL_USERNAME'@'localhost' IDENTIFIED BY 'NEW_RELIC_MYSQL_PASSWORD';
173+
GRANT REPLICATION CLIENT ON *.* TO 'newrelic'@'localhost';
174+
GRANT SELECT ON *.* TO 'newrelic'@'localhost';
175+
EOT
176+
177+
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-drop-user.sql
178+
sudo sed -i 's/NEW_RELIC_MYSQL_USERNAME/'$NEW_RELIC_MYSQL_USERNAME'/g' /tmp/sql-create-user.sql
179+
sudo sed -i 's/NEW_RELIC_MYSQL_PASSWORD/'$NEW_RELIC_MYSQL_PASSWORD'/g' /tmp/sql-create-user.sql
180+
181+
if [[ $MYSQL_ROOT_PASSWORD -gt 0 && "$NEW_RELIC_ASSUME_YES" != "true" ]] ; then
182182
TRIES=0
183+
printf "MySql credential with permission to create user, and grant select/replication permission is required, please enter it below when prompted\n"
183184
SQL_OUTPUT_ERROR=1
185+
184186
while [ $TRIES -lt {{.MAX_RETRIES}} ] && [ $SQL_OUTPUT_ERROR -gt 0 ] ;
185187
do
186188
if [ $TRIES -gt 0 ]; then printf "\nPlease try again\n"; fi
@@ -189,6 +191,7 @@ install:
189191
read -r USERNAME
190192
USERNAME=${USERNAME:-root}
191193
194+
sudo mysql -u $USERNAME --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
192195
EXEC_OUTPUT=$(eval sudo mysql -u $USERNAME --port $NR_CLI_DB_PORT -p$NR_CLI_DB_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
193196
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
194197
SQL_OUTPUT_ERROR=$(echo -n $EXEC_OUTPUT | grep "ERROR" | wc -l)
@@ -199,26 +202,35 @@ install:
199202
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPerhaps the MySql root DB password is incorrect.\nPlease either re-run the installation with the correct root DB password, or follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/\n\n" >&2
200203
sudo cat /tmp/sql-create-user.sql
201204
sudo rm -f /tmp/sql-create-user.sql
205+
sudo rm -f /tmp/sql-drop-user.sql
202206
printf "\n"
203207
echo -e "detail:"$EXEC_OUTPUT
204208
printf "\n"
205209
exit 131
206210
fi
207211
else
208-
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NR_CLI_DB_PORT < /tmp/sql-create-user.sql 2>&1)
212+
if [[ -z "$NEW_RELIC_MYSQL_ROOT_PASSWORD" ]]; then
213+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-drop-user.sql &> /dev/null ||:
214+
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT < /tmp/sql-create-user.sql 2>&1)
215+
else
216+
sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-drop-user.sql &> /dev/null ||:
217+
EXEC_OUTPUT=$(eval sudo mysql -u root --port $NEW_RELIC_MYSQL_PORT -p$NEW_RELIC_MYSQL_ROOT_PASSWORD < /tmp/sql-create-user.sql 2>&1)
218+
fi
209219
echo "MySqlOutput:"$EXEC_OUTPUT | sudo tee -a {{.NEW_RELIC_CLI_LOG_FILE_PATH}} > /dev/null
210220
SQL_OUTPUT_ERROR=$(echo -n $EXEC_OUTPUT | grep "ERROR" | wc -l)
211221
if [ $SQL_OUTPUT_ERROR -gt 0 ] ; then
212222
printf "\n\nThis installation was unable to create the required newrelic user.\nWe attempted to create the user with the SQL statement below.\nPlease follow the manual installation steps documented at https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/mysql-monitoring-integration/\n\n" >&2
213223
sudo cat /tmp/sql-create-user.sql
214224
sudo rm -f /tmp/sql-create-user.sql
225+
sudo rm -f /tmp/sql-drop-user.sql
215226
printf "\n"
216227
echo -e "detail:"$EXEC_OUTPUT
217228
printf "\n"
218229
exit 131
219230
fi
220231
fi
221232
sudo rm -f /tmp/sql-create-user.sql
233+
sudo rm -f /tmp/sql-drop-user.sql
222234
223235
# Install the integration
224236
sudo mkdir -p "/etc/newrelic-infra/integrations.d"

0 commit comments

Comments
 (0)