Skip to content

Commit

Permalink
[MDEV-14978] Client programs to use $MYSQL_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.

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 Mar 16, 2023
1 parent 090e5d8 commit 138e8bf
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 1 deletion.
7 changes: 6 additions & 1 deletion client/mysqladmin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ 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 @@ -373,6 +373,11 @@ int main(int argc,char *argv[])
int error= 0, temp_argc;
MYSQL mysql;
char **commands, **save_argv, **temp_argv;
char *tmp;

tmp= (char *) getenv("MYSQL_HOST");
if (tmp && !host)
host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

MY_INIT(argv[0]);
sf_leaking_memory=1; /* don't report memory leaks on early exits */
Expand Down
5 changes: 5 additions & 0 deletions client/mysqlbinlog.cc
Original file line number Diff line number Diff line change
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= (char *) getenv("MYSQL_HOST");
if (tmp && !host)
host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

if ((ho_error=handle_options(argc, argv, my_options, get_one_option)))
{
Expand Down
5 changes: 5 additions & 0 deletions client/mysqlcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,14 @@ get_one_option(const struct my_option *opt,

static int get_options(int *argc, char ***argv)
{
char *tmp;
int ho_error;
DBUG_ENTER("get_options");

tmp= (char *) getenv("MYSQL_HOST");
if (tmp && !current_host)
current_host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

if (*argc == 1)
{
usage();
Expand Down
6 changes: 6 additions & 0 deletions client/mysqldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,15 @@ 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= (char *) getenv("MYSQL_HOST");
if (tmp && !current_host)
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
5 changes: 5 additions & 0 deletions client/mysqlimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,13 @@ get_one_option(const struct my_option *opt, const char *argument,

static int get_options(int *argc, char ***argv)
{
char *tmp;
int ho_error;

tmp= (char *) getenv("MYSQL_HOST");
if (tmp && !current_host)
current_host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);
if (debug_info_flag)
Expand Down
5 changes: 5 additions & 0 deletions client/mysqlshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ get_one_option(const struct my_option *opt, const char *argument,
static void
get_options(int *argc,char ***argv)
{
char *tmp;
int ho_error;

tmp= (char *) getenv("MYSQL_HOST");
if (tmp && !host)
host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

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

Expand Down
5 changes: 5 additions & 0 deletions client/mysqlslap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1228,10 +1228,15 @@ build_select_string(my_bool key)
static int
get_options(int *argc,char ***argv)
{
char *tmp;
int ho_error;
char *tmp_string;
MY_STAT sbuf; /* Stat information for the data file */

tmp= (char *) getenv("MYSQL_HOST");
if (tmp && !host)
host= my_strdup(PSI_NOT_INSTRUMENTED, tmp, MYF(MY_WME));

DBUG_ENTER("get_options");
if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
exit(ho_error);
Expand Down
24 changes: 24 additions & 0 deletions mysql-test/suite/client/client-env-variable.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE DATABASE env_variable;
USE env_variable;
CREATE TABLE pet (name VARCHAR(20));
show binary logs;
Log_name File_size
master-bin.000001 635
+----+------+-----------+--------------+---------+------+----------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+------+-----------+--------------+---------+------+----------+------------------+----------+
| 5 | root | localhost | env_variable | Sleep | 0 | | | 0.000 |
| 6 | root | localhost | | Query | 0 | starting | show processlist | 0.000 |
+----+------+-----------+--------------+---------+------+----------+------------------+----------+
env_variable.pet OK
env_variable.pet: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
Database: env_variable
+--------+
| Tables |
+--------+
| pet |
+--------+
****************
# Setting MYSQL_HOST environment variable
Done
DROP DATABASE env_variable;
65 changes: 65 additions & 0 deletions mysql-test/suite/client/client-env-variable.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- source include/have_log_bin.inc

# Set up environment varibles for client programs
--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 database for the client programs
CREATE DATABASE env_variable;
USE env_variable;
CREATE TABLE pet (name VARCHAR(20));
show binary logs;

# 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
--exec $MYSQLBINLOG $options --read-from-remote-server master-bin.000001 > /dev/null 2>&1
--exec $MYSQLCHECK $options -c --databases env_variable
--exec $MYSQLDUMP $options env_variable > $MYSQL_TMP_DIR/tmp.sql
--exec $MYSQLIMPORT $options env_variable --local $MYSQL_TMP_DIR/pet
--exec $MYSQLSHOW $options env_variable
--exec $MYSQLSLAP $options > /dev/null 2>&1

--echo ****************
--echo # Setting MYSQL_HOST environment variable
--let MYSQL_HOST=nonexistent-sever

# Now run the same command as before with MYSQL_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

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

--error 2
--exec $MYSQLCHECK $options -c --databases env_variable

--error 2
--exec $MYSQLDUMP $options env_variable > $MYSQL_TMP_DIR/tmp.sql

--error 1
--exec $MYSQLIMPORT $options env_variable $MYSQL_TMP_DIR/pet

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

# Clean up
--echo Done
DROP DATABASE env_variable;

0 comments on commit 138e8bf

Please sign in to comment.