I want to script a rather simple create database operation. Thing is, I have to provide the password for that database. I would like to do this with an environment variable, but the simple approach dose not work:
mailpswd=charlie
mysql -u root -p
CREATE DATABASE mailfix; CREATE USER 'mailfix'@'localhost' IDENTIFIED BY $mailpswd; GRANT ALL PRIVILEGES ON `mailfix` . * TO 'mailfix'@'localhost';
Of course the mysql command needs the mysql root password, but that is as expected. But mysql will not process $mailpswd, not surprisingly.
I know I could create a file with these commands, sed the password into the file, then pipe thr file into mysql. I would rather do this directly without a temp file.
thanks
Date: Tuesday, March 14, 2017 14:53:01 -0700 From: Robert Moskowitz rgm@htt-consult.com
I want to script a rather simple create database operation. Thing is, I have to provide the password for that database. I would like to do this with an environment variable, but the simple approach dose not work:
mailpswd=charlie
mysql -u root -p
CREATE DATABASE mailfix; CREATE USER 'mailfix'@'localhost' IDENTIFIED BY $mailpswd; GRANT ALL PRIVILEGES ON `mailfix` . * TO 'mailfix'@'localhost';
Of course the mysql command needs the mysql root password, but that is as expected. But mysql will not process $mailpswd, not surprisingly.
I know I could create a file with these commands, sed the password into the file, then pipe thr file into mysql. I would rather do this directly without a temp file.
Using the "-e --execute" option you can execute mysql/mariadb commands from the command line, one at a time. You don't have to put them into a file first. That should allow you to do what you're after, including doing variable substitution of the password on the line you want to execute. You can also put the mysql root pw on that line with substitution so no prompting is involved.
On 03/14/2017 06:29 PM, Richard wrote:
Date: Tuesday, March 14, 2017 14:53:01 -0700 From: Robert Moskowitz rgm@htt-consult.com
I want to script a rather simple create database operation. Thing is, I have to provide the password for that database. I would like to do this with an environment variable, but the simple approach dose not work:
mailpswd=charlie
mysql -u root -p
CREATE DATABASE mailfix; CREATE USER 'mailfix'@'localhost' IDENTIFIED BY $mailpswd; GRANT ALL PRIVILEGES ON `mailfix` . * TO 'mailfix'@'localhost';
Of course the mysql command needs the mysql root password, but that is as expected. But mysql will not process $mailpswd, not surprisingly.
I know I could create a file with these commands, sed the password into the file, then pipe thr file into mysql. I would rather do this directly without a temp file.
Using the "-e --execute" option you can execute mysql/mariadb commands from the command line, one at a time. You don't have to put them into a file first. That should allow you to do what you're after, including doing variable substitution of the password on the line you want to execute. You can also put the mysql root pw on that line with substitution so no prompting is involved.
I saw this, and at first did not like it, as how to manage the mysql root password, so I found how to use here doc:
mysql -u root -p <<EOF || exit 1 CREATE DATABASE postfix; CREATE USER postfix@localhost IDENTIFIED BY "$Postfix_Database_Password"; GRANT ALL PRIVILEGES ON postfix.* TO postfix@localhost; EOF
next refinement is:
mysql -u root -p$mysql_root_Password <<EOF || exit 1 CREATE DATABASE postfix; CREATE USER postfix@localhost IDENTIFIED BY "$Postfix_Database_Password"; GRANT ALL PRIVILEGES ON postfix.* TO postfix@localhost; EOF
You can just put the credentials in a my.cnf file to avoid entering them all the time
e.g. /root/.my.cnf
[client] user=root password=thepassword
-- Sent from the Delta quadrant using Borg technology!
Nux! www.nux.ro
----- Original Message -----
From: "Robert Moskowitz" rgm@htt-consult.com To: "CentOS mailing list" centos@centos.org Sent: Wednesday, 15 March, 2017 04:07:36 Subject: Re: [CentOS] Using environment variables in mariadb
On 03/14/2017 06:29 PM, Richard wrote:
Date: Tuesday, March 14, 2017 14:53:01 -0700 From: Robert Moskowitz rgm@htt-consult.com
I want to script a rather simple create database operation. Thing is, I have to provide the password for that database. I would like to do this with an environment variable, but the simple approach dose not work:
mailpswd=charlie
mysql -u root -p
CREATE DATABASE mailfix; CREATE USER 'mailfix'@'localhost' IDENTIFIED BY $mailpswd; GRANT ALL PRIVILEGES ON `mailfix` . * TO 'mailfix'@'localhost';
Of course the mysql command needs the mysql root password, but that is as expected. But mysql will not process $mailpswd, not surprisingly.
I know I could create a file with these commands, sed the password into the file, then pipe thr file into mysql. I would rather do this directly without a temp file.
Using the "-e --execute" option you can execute mysql/mariadb commands from the command line, one at a time. You don't have to put them into a file first. That should allow you to do what you're after, including doing variable substitution of the password on the line you want to execute. You can also put the mysql root pw on that line with substitution so no prompting is involved.
I saw this, and at first did not like it, as how to manage the mysql root password, so I found how to use here doc:
mysql -u root -p <<EOF || exit 1 CREATE DATABASE postfix; CREATE USER postfix@localhost IDENTIFIED BY "$Postfix_Database_Password"; GRANT ALL PRIVILEGES ON postfix.* TO postfix@localhost; EOF
next refinement is:
mysql -u root -p$mysql_root_Password <<EOF || exit 1 CREATE DATABASE postfix; CREATE USER postfix@localhost IDENTIFIED BY "$Postfix_Database_Password"; GRANT ALL PRIVILEGES ON postfix.* TO postfix@localhost; EOF
CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos