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 18, 2023
1 parent d20a96f commit 645b308
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 15 deletions.
8 changes: 5 additions & 3 deletions client/mysqladmin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -245,7 +245,6 @@ static const char *load_default_groups[]=
my_bool
get_one_option(const struct my_option *opt, const char *argument, const char *filename)
{

/* Track when protocol is set via CLI to not force overrides */
static my_bool ignore_protocol_override = FALSE;

Expand Down Expand Up @@ -374,6 +373,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
9 changes: 7 additions & 2 deletions client/mysqlbinlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1485,8 +1485,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -2286,6 +2286,11 @@ get_one_option(const struct my_option *opt, const char *argument, const char *fi
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
7 changes: 5 additions & 2 deletions client/mysqlcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -437,6 +437,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
9 changes: 7 additions & 2 deletions client/mysqldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -1121,9 +1121,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
7 changes: 5 additions & 2 deletions client/mysqlimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -326,6 +326,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
7 changes: 5 additions & 2 deletions client/mysqlshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -412,6 +412,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
7 changes: 5 additions & 2 deletions client/mysqlslap.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ 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. If host is not specified, connect to localhost by default or $MARIADB_HOST",
&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 @@ -1232,6 +1232,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
6 changes: 6 additions & 0 deletions mysql-test/suite/client/client-env-variable.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
USE test;
CREATE TABLE pet (name VARCHAR(20));
****************
# Setting MARIADB_HOST environment variable
Done
DROP TABLE pet;
76 changes: 76 additions & 0 deletions mysql-test/suite/client/client-env-variable.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- 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 MYSQLADMIN = $MYSQL_BINDIR/client//mariadb-admin
--let MYSQLBINLOG = $MYSQL_BINDIR/client//mariadb-binlog
--let MYSQLCHECK = $MYSQL_BINDIR/client//mariadb-check
--let MYSQLDUMP = $MYSQL_BINDIR/client//mariadb-dump
--let MYSQLIMPORT = $MYSQL_BINDIR/client//mariadb-import
--let MYSQLSHOW = $MYSQL_BINDIR/client//mariadb-show
--let MYSQLSLAP = $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=16000 --socket=$MYSQL_TMP_DIR/mysqld.1.sock

# Run client programs using the default `localhost`
--exec $MYSQLADMIN $options processlist > /dev/null 2>&1
--exec $MYSQLBINLOG $options --read-from-remote-server master-bin.000001 > /dev/null 2>&1
--exec $MYSQLCHECK $options -c --databases test > /dev/null 2>&1
--exec $MYSQLDUMP $options test > $MYSQL_TMP_DIR/tmp.sql > /dev/null 2>&1
--exec $MYSQLIMPORT $options test --local $MYSQL_TMP_DIR/pet > /dev/null 2>&1
--exec $MYSQLSHOW $options test > /dev/null 2>&1
--exec $MYSQLSLAP $options > /dev/null 2>&1

--echo ****************
--echo # Setting MARIADB_HOST environment variable
--let MARIADB_HOST=nonexistent-server

# Now run the same command as before with MARIADB_HOST environment variable
# 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 $MYSQLADMIN $options processlist > /dev/null 2>&1

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

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

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

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

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

# Run the same command but with '--host' to verify command line input overrides env variable
--exec $MYSQLADMIN $options --host localhost processlist > /dev/null 2>&1
--exec $MYSQLBINLOG $options --host localhost --read-from-remote-server master-bin.000001 > /dev/null 2>&1
--exec $MYSQLCHECK $options --host localhost -c --databases test > /dev/null 2>&1
--exec $MYSQLDUMP $options --host localhost test > $MYSQL_TMP_DIR/tmp.sql > /dev/null 2>&1
--exec $MYSQLIMPORT $options --host localhost test --local $MYSQL_TMP_DIR/pet > /dev/null 2>&1
--exec $MYSQLSHOW $options --host localhost test > /dev/null 2>&1
--exec $MYSQLSLAP $options --host localhost > /dev/null 2>&1

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

0 comments on commit 645b308

Please sign in to comment.