Skip to content

Commit

Permalink
[MDEV-14978] Client programs to use $MARIADB_HOST consistently
Browse files Browse the repository at this point in the history
Only `mysql` client program was using $MYSQL_HOST as the default host.
Add the same feature in most other client programs but using
$MARIADB_HOST instead.

All new code of the whole pull request, including one or several files that are
either new files or modified ones, are contributed under the BSD-new license. I
am contributing on behalf of my employer Amazon Web Services, Inc.
  • Loading branch information
oceanli-hub committed Apr 9, 2024
1 parent 1e889a6 commit e9cc3dd
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 17 deletions.
10 changes: 7 additions & 3 deletions client/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1729,8 +1729,9 @@ static struct my_option my_long_options[] =
&opt_local_infile, &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
{"no-beep", 'b', "Turn off beep on error.", &opt_nobeep,
&opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, $MYSQL_HOST, and then localhost",
&current_host, &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"html", 'H', "Produce HTML output.", &opt_html, &opt_html,
0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"xml", 'X', "Produce XML output.", &opt_xml, &opt_xml, 0,
Expand Down Expand Up @@ -2130,7 +2131,10 @@ static int get_options(int argc, char **argv)
int ho_error;
MYSQL_PARAMETERS *mysql_params= mysql_get_parameters();

tmp= (char *) getenv("MYSQL_HOST");
//MARIADB_HOST will be preferred over MYSQL_HOST.
tmp= getenv("MARIADB_HOST");
if (tmp == NULL)
tmp= getenv("MYSQL_HOST");
if (tmp)
current_host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

Expand Down
8 changes: 6 additions & 2 deletions client/mysqladmin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ static struct my_option my_long_options[] =
&default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &host, &host, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&host, &host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"local", 'l', "Local command, don't write to binlog.",
&opt_local, &opt_local, 0, GET_BOOL, NO_ARG, 0, 0, 0,
0, 0, 0},
Expand Down Expand Up @@ -329,6 +330,9 @@ int main(int argc,char *argv[])
MYSQL mysql;
char **commands, **save_argv, **temp_argv;

if (host == NULL)
host= getenv("MARIADB_HOST");

MY_INIT(argv[0]);
sf_leaking_memory=1; /* don't report memory leaks on early exits */

Expand Down
10 changes: 8 additions & 2 deletions client/mysqlbinlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1484,8 +1484,9 @@ static struct my_option my_options[] =
{"hexdump", 'H', "Augment output with hexadecimal and ASCII event dump.",
&opt_hexdump, &opt_hexdump, 0, GET_BOOL, NO_ARG,
0, 0, 0, 0, 0, 0},
{"host", 'h', "Get the binlog from server.", &host, &host,
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Get the binlog from server. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&host, &host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"local-load", 'l', "Prepare local temporary files for LOAD DATA INFILE in the specified directory.",
&dirname_for_local_load, &dirname_for_local_load, 0,
GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
Expand Down Expand Up @@ -2269,6 +2270,11 @@ get_one_option(const struct my_option *opt, const char *argument,
static int parse_args(int *argc, char*** argv)
{
int ho_error;
char *tmp;

tmp= getenv("MARIADB_HOST");
if (tmp && host == NULL)
host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

if ((ho_error=handle_options(argc, argv, my_options, get_one_option)))
{
Expand Down
8 changes: 6 additions & 2 deletions client/mysqlcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ static struct my_option my_long_options[] =
0, 0 },
{"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
{"host",'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host",'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&current_host, &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"medium-check", 'm',
"Faster than extended-check, but only finds 99.99 percent of all errors. Should be good enough for most cases.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
Expand Down Expand Up @@ -407,6 +408,9 @@ static int get_options(int *argc, char ***argv)
int ho_error;
DBUG_ENTER("get_options");

if (current_host == NULL)
current_host= getenv("MARIADB_HOST");

if (*argc == 1)
{
usage();
Expand Down
10 changes: 8 additions & 2 deletions client/mysqldump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,9 @@ static struct my_option my_long_options[] =
{"hex-blob", OPT_HEXBLOB, "Dump binary strings (BINARY, "
"VARBINARY, BLOB) in hexadecimal format.",
&opt_hex_blob, &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&current_host, &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"ignore-database", OPT_IGNORE_DATABASE,
"Do not dump the specified database. To specify more than one database to ignore, "
"use the directive multiple times, once for each database. Only takes effect "
Expand Down Expand Up @@ -1094,9 +1095,14 @@ get_one_option(const struct my_option *opt,

static int get_options(int *argc, char ***argv)
{
char *tmp;
int ho_error;
MYSQL_PARAMETERS *mysql_params= mysql_get_parameters();

tmp= getenv("MARIADB_HOST");
if (tmp && current_host == NULL)
current_host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

opt_max_allowed_packet= *mysql_params->p_max_allowed_packet;
opt_net_buffer_length= *mysql_params->p_net_buffer_length;

Expand Down
8 changes: 6 additions & 2 deletions client/mysqlimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ static struct my_option my_long_options[] =
0, 0, 0, 0},
{"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG, NO_ARG,
0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&current_host, &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"ignore", 'i', "If duplicate unique key was found, keep old row.",
&ignore, &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"ignore-foreign-keys", 'k',
Expand Down Expand Up @@ -295,6 +296,9 @@ static int get_options(int *argc, char ***argv)
{
int ho_error;

if (current_host == NULL)
current_host= getenv("MARIADB_HOST");

if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);
if (debug_info_flag)
Expand Down
8 changes: 6 additions & 2 deletions client/mysqlshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ static struct my_option my_long_options[] =
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &host, &host, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&host, &host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"status", 'i', "Shows a lot of extra information about each table.",
&opt_status, &opt_status, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
0, 0},
Expand Down Expand Up @@ -363,6 +364,9 @@ get_options(int *argc,char ***argv)
{
int ho_error;

if (host == NULL)
host= getenv("MARIADB_HOST");

if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);

Expand Down
8 changes: 6 additions & 2 deletions client/mysqlslap.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,9 @@ static struct my_option my_long_options[] =
"engine after a `:', like memory:max_row=2300",
&default_engine, &default_engine, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &host, &host, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host. Defaults in the following order: "
"$MARIADB_HOST, and then localhost",
&host, &host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"init-command", OPT_INIT_COMMAND,
"SQL Command to execute when connecting to MariaDB server. Will "
"automatically be re-executed when reconnecting.",
Expand Down Expand Up @@ -1183,6 +1184,9 @@ get_options(int *argc,char ***argv)
char *tmp_string;
MY_STAT sbuf; /* Stat information for the data file */

if (host == NULL)
host= getenv("MARIADB_HOST");

DBUG_ENTER("get_options");
if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);
Expand Down
5 changes: 5 additions & 0 deletions mysql-test/suite/client/client-env-variable.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!include include/default_my.cnf

[ENV]
MARIADB_HOST=localhost
MYSQL_HOST=some_server
7 changes: 7 additions & 0 deletions mysql-test/suite/client/client-env-variable.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
USE test;
CREATE TABLE pet (name VARCHAR(20));
localhost
****************
nonexistent-server
Done
DROP TABLE pet;
87 changes: 87 additions & 0 deletions mysql-test/suite/client/client-env-variable.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
-- source include/have_log_bin.inc

# Set up environment varibles for client programs
# The environment variables for client programs have default options file
# They interfere with the MTR test so I am creating new variables for them

--let MARIADB = $MYSQL_BINDIR/client//mariadb
--let MARIADBADMIN = $MYSQL_BINDIR/client//mariadb-admin
--let MARIADBBINLOG = $MYSQL_BINDIR/client//mariadb-binlog
--let MARIADBCHECK = $MYSQL_BINDIR/client//mariadb-check
--let MARIADBDUMP = $MYSQL_BINDIR/client//mariadb-dump
--let MARIADBIMPORT = $MYSQL_BINDIR/client//mariadb-import
--let MARIADBSHOW = $MYSQL_BINDIR/client//mariadb-show
--let MARIADBSLAP = $MYSQL_BINDIR/client//mariadb-slap

# Creating a table for the client programs
USE test;
CREATE TABLE pet (name VARCHAR(20));

# Creating a data file for mysqlimport
write_file $MYSQL_TMP_DIR/pet;
buster
bob
EOF

# Options for client program
--let $options = --user=root --port=$MASTER_MYPORT --disable-ssl-verify-server-cert

# Check to see if environment variable is defined
# MARIADB_HOST is defined in client-env-variable.cnf
--echo $MARIADB_HOST

# Positive test for client program with MARIADB_HOST
--exec $MARIADB $options -e "SHOW DATABASES;" > /dev/null 2>&1
--exec $MARIADBADMIN $options processlist > /dev/null 2>&1
--exec $MARIADBBINLOG $options --read-from-remote-server master-bin.000001 > /dev/null 2>&1
--exec $MARIADBCHECK $options -c --databases test > /dev/null 2>&1
--exec $MARIADBDUMP $options test > $MYSQL_TMP_DIR/tmp.sql > /dev/null 2>&1
--exec $MARIADBIMPORT $options test --local $MYSQL_TMP_DIR/pet > /dev/null 2>&1
--exec $MARIADBSHOW $options test > /dev/null 2>&1
--exec $MARIADBSLAP $options > /dev/null 2>&1

#Set up negative test with invalid server
--echo ****************
--let MARIADB_HOST=nonexistent-server
--echo $MARIADB_HOST

# Now run the same command as in the postive test case
# Client programs are expected to fail since the server does not exist
# Some client program fails with error 1 and some fails wtih error 2

--error 1
--exec $MARIADB $options -e "SHOW DATABASES;" > /dev/null 2>&1

--error 1
--exec $MARIADBADMIN $options processlist > /dev/null 2>&1

--error 1
--exec $MARIADBBINLOG $options --read-from-remote-server master-bin.000001 > /dev/null 2>&1

--error 2
--exec $MARIADBCHECK $options -c --databases test > /dev/null 2>&1

--error 2
--exec $MARIADBDUMP $options test > $MYSQL_TMP_DIR/tmp.sql > /dev/null 2>&1

--error 1
--exec $MARIADBIMPORT $options test $MYSQL_TMP_DIR/pet > /dev/null 2>&1

--error 1
--exec $MARIADBSLAP $options > /dev/null 2>&1

# Run the same command but with '--host' to verify command line input overrides env variable
--exec $MARIADB $options --host localhost -e "SHOW DATABASES;" > /dev/null 2>&1
--exec $MARIADBADMIN $options --host localhost processlist > /dev/null 2>&1
--exec $MARIADBBINLOG $options --host localhost --read-from-remote-server master-bin.000001 > /dev/null 2>&1
--exec $MARIADBCHECK $options --host localhost -c --databases test > /dev/null 2>&1
--exec $MARIADBDUMP $options --host localhost test > $MYSQL_TMP_DIR/tmp.sql > /dev/null 2>&1
--exec $MARIADBIMPORT $options --host localhost test --local $MYSQL_TMP_DIR/pet > /dev/null 2>&1
--exec $MARIADBSHOW $options --host localhost test > /dev/null 2>&1
--exec $MARIADBSLAP $options --host localhost > /dev/null 2>&1

# Clean up
--echo Done
DROP TABLE pet;
--remove_file $MYSQL_TMP_DIR/tmp.sql
--remove_file $MYSQL_TMP_DIR/pet

0 comments on commit e9cc3dd

Please sign in to comment.