I am executing this line in inno setup, but i am getting and exit code 2
;Setting root password default root (blank). ex : mypass4u#
Filename: "{app}\mysql\bin\mysqladmin.exe"; \
Parameters: "-u root -e ""update mysql.user set password=PASSWORD('mypass4u#') where user='root';"""; \
StatusMsg: "Setting password root"; \
Flags: runhidden;
I get the following message in the debug window
[11:56:54.387] -- Run entry -- [11:56:54.392] Run as: Current user
[11:56:54.396] Type: Exec
[11:56:54.400] Filename: C:\Program Files (x86)\Company\Myapp\mysql\bin\mysqladmin.exe
[11:56:54.405] Parameters: -u root -e "update mysql.user set
password=PASSWORD('mypass4u#') where user='root';"
[11:56:54.758] Process exit code: 2
What could be causing this error
I assume you wanted to use mysql.exe, not mysqladmin.exe.
Related
The last line of the script was not executed.
I tried to execute the code manually on the instance created and it was successful.
#!/bin/bash
#install tools
apt-get update -y
apt-get install mysql-client -y
#Create MySQL config file
echo "[mysql]" >> ~/.my.cnf
echo "user = poc5admin" >> ~/.my.cnf
echo "password = poc5password" >> ~/.my.cnf
#test
echo "endpoint = ${rds_endpoint}" >> ~/variables
hostip=$(hostname -I)
endpoint=${rds_endpoint}
echo "$hostip" >> ~/variables
#I have created a table here but I will remove the code since it is unnecessary...
#Create User
echo "CREATE USER 'poc5user'#'%' IDENTIFIED BY 'poc5pass';" >> ~/mysqlscript.sql
echo "GRANT EVENT ON * . * TO 'poc5user'#'%';" >> ~/mysqlscript.sql
cp mysqlscript.sql /home/ubuntu/mysqlscript.sql
mysql -h $endpoint -u poc5admin < ~/mysqlscript.sql
Expected result: There should be a Database, Table and User created on the RDS instance.
You can insert or create Database like this from the bash script but it is not recommended an approach to work with RDS. better to place your data over s3 and import from the s3.
Here is the example, that will create DB
resource "aws_db_instance" "db" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "foobarbaz"
parameter_group_name = "default.mysql5.7"
s3_import {
source_engine = "mysql"
source_engine_version = "5.6"
bucket_name = "mybucket"
bucket_prefix = "backups"
ingestion_role = "arn:aws:iam::1234567890:role/role-xtrabackup-rds-restore"
}
}
~/.my.cnf why you need this? better to place these script in the s3 file.
second thing, If you still interesting to run from your local environment then you can insert from local-exec
resource "null_resource" "main_db_update_table" {
provisioner "local-exec" {
on_failure = "fail"
interpreter = ["/bin/bash", "-c"]
command = <<EOT
mysql -h ${aws_rds_cluster.db.endpoint} -u your_username -pyour_password your_db < mysql_script.sql
EOT
}
}
But better to with s3.
If you want to import from remote, you can explore remote-exec.
With user-data, you can do this but it seems your MySQL script not generating properly. better to cp script to remote and then run with local exec in remote.
There is no such thing as terraform "user_data". User data is a bootstrap script for the EC2 instances which you can use to install software/binaries or to execute your script at the boot time.
The script will be executed by the cloud-init, not by the terraform itself. The responsibility of the terraform is to set user-data for the ec2 instances.
You may check the cloud-init output logs which should have the result of your user-data script also.
From your code, I am not able to understand which step you have copied the below file.
cp mysqlscript.sql /home/ubuntu/mysqlscript.sql
mysql -h $endpoint -u poc5admin < ~/mysqlscript.sql
I am assuming that you are creating a new server and it does not have any file.
Thank you for your inputs. I have found an answer by moving the config file to /etc/mysql/my.cnf and then executing
mysql -h $endpoint -u poc5admin < ~/mysqlscript.sql
With laravel 5.8 envoy command I deploy on remote server and I set password in command line, like:
envoy run Hostels2Deploy --lardeployer_password=111 --app_version=0.105a
and envoy file:
#setup
$server_login_user= 'lardeployer';
$lardeployer_password = isset($lardeployer_password) ? $lardeployer_password : "Not Defined";
#endsetup
#servers(['dev' => $server_login_user.':'.$lardeployer_password.'#NNN.NN.NNN.N'])
#task('clean_old_releases')
echo "Step # 81";
echo 'The password is: {{ $lardeployer_password }}';
echo 'The $server_login_user is: {{ $server_login_user }}';
echo "Step # 00 app_version ::{{ $app_version }}";
cd {{ $release_number_dir }}
# php artisan envoy:delete-old-versions Hostels2Deployed
#endtask
#macro('Hostels2Deploy',['on'=>'dev'])
clean_old_releases
#endmacro
With credentials in #servers block I expected I will not have to enter password manually, but in command line I see prompt to enter
password. I output $server_login_user and $lardeployer_password vars and they have valid values.
Which is valid path ?
I found a decision with ssh keys in /home/user/.ssh/config of my OS to add line :
Host laravelserver
IdentityFile ~/.ssh/id_rsa
HostName NNN.NN.NNN.N
Port 22
User lardeployer
and in envoy file to connect to this server like :
#servers(['dev' => ['laravelserver'] )
Aslo on remote user in file authorized_keys lardeployer's public key must be added.
and restart the sevice :
sudo systemctl restart ssh
I am using the script below to install mysql with my application, but it always fails to create the database and load the database script. The debugger returns an error code of 1, I dont find any other message as regards the cause of the failure anywhere
when i check event viewer, it saw service successfully created and started, which is true, but I dont understand why the database is not created.
[Run]
;------------------install service MySQL --------------------------------
;Filename: "{app}\mysql\bin\mysqld.exe"; Parameters: "--install"; WorkingDir: "{app}\mysql\bin"; StatusMsg: "Installing the MySQL"; Description: "Installing MySQL"; Flags: runhidden; Check: MySQL_Is;
Filename: msiexec; Parameters: "/i ""{app}\mysql-5.5.11-win32.msi"" /qn INSTALLDIR=""{app}\mysql"" DATADIR=""{app}\mysql\data"" "; WorkingDir:{app}; StatusMsg: Please wait while we install; Flags: runhidden
;---------------------CONFIGURING ----------------------------
Filename: {app}\mysql\bin\MySQLInstanceConfig.exe; Parameters:"-i -q ""-l{app}\mysql\mysql_install_log.txt"" ""-nMySQL Server 5.5"" ""-p{app}\mysql"" -v5.5.23 ""-t{app}\mysql\my-template.ini"" ""-c{app}\mysql\mysql.ini"" ServerType=SERVER DatabaseType=MIXED Port=3311 StrictMode=yes ConnectionCount=15 Charset=utf8 ServiceName=MySQL55 AddBinToPath=yes RootPassword=root4u SkipNetworking=no"; WorkingDir: {app}; StatusMsg: Configuring MySQL services; Description: Configuring MySQL Service; Flags: runhidden
;--------------------- load Database script
;Filename: "{app}\mysql\bin\mysql.exe"; Parameters: "-u root -proot4u -h localhost -e ""source {app}\items_db.sql"""; WorkingDir: {app}; StatusMsg: Loading Database Initial Data; Flags: runhidden waituntilterminated;
Filename: {app}\{#AppExeName}; Description: {cm:LaunchProgram,{#AppName}}; Flags: nowait postinstall skipifsilent
[UninstallRun]
Filename: {sys}\net.exe; Parameters: "stop ""MySQL55"""; StatusMsg: "Stopping MySQL Service ..."; Flags: runhidden;
Filename: "{app}\mysql\bin\mysqld.exe"; Parameters: "remove ""MySQL55"""; StatusMsg: "Deleting MySQL Service ..."; Flags: runhidden;
Filename: msiexec; Parameters: "/x ""{app}\mysql-5.5.11-win32.msi"" ";
=========================The database script==============
/* Tables */
DROP TABLE IF EXISTS clothes;
CREATE TABLE clothes (
id int AUTO_INCREMENT NOT NULL,
name varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci,
PRIMARY KEY (id)
) ENGINE = MyISAM;
Insert Into clothes (Id,Name) values (1,"Shirt");
So after hours of debugging and searching the web, I found a weird behaviour
mysql does not allow backlash in the source path parameter when executing from sql statements from file.
;Filename: "{app}\mysql\bin\mysql.exe"; Parameters: "-u root -proot4u -h localhost -e ""source {app}\items_db.sql"""; WorkingDir: {app}; StatusMsg: Loading Database Initial Data; Flags: runhidden waituntilterminated;
so i started looking for a way around this
;Filename: "{app}\mysql\bin\mysql.exe"; Parameters: "-u root -proot4u -h localhost -e ""source {app}\{code:GetScriptData}"""; WorkingDir: {app}; StatusMsg: Loading Database Initial Data; Flags: runhidden waituntilterminated;
and in the code section
[Code]
function GetScriptData(Value: string): string;
begin
Result := ExpandConstant('{app}') +'/items_db.sql';
StringChangeEx(Result, '\', '/', True);
end;
My bash script won't crate the Database. What am I doing wrong here?
Please have a look:
#!/bin/bash -x
set -x
function deebee() {
EXPECTED_ARGS=2
E_BADARGS=65
MYSQL=`which mysql`
Q1="CREATE DATABASE IF NOT EXISTS $1;"
Q2="GRANT USAGE ON *.* TO $2#localhost IDENTIFIED BY '$3';"
Q3="GRANT ALL PRIVILEGES ON $1.* TO $2#localhost;"
Q4="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}"
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 dbname dbuser dbpass"
exit $E_BADARGS
fi
$MYSQL -uroot -p -e "$SQL"
}
deebee $1 $2 $3
I'm calling the script as I've put it in a function, but it just spits out the expected arguments telling me the syntax, i.e that I should type in the bashscrip name, dbname, dbuser dbpass, but obviously there' something wrong with the script of my login permissions or user so that I can't automate this... What's going on, I'd love to know!
Thanks!
What I would do :
creating a mysql config file with credentials (easier to automate now) :
cat ~/.my.cnf
[client]
host = localhost
user = root
password = xxx
And the script :
#!/bin/bash
set -x
deebee() {
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=$(type -p mysql)
if (($# != $EXPECTED_ARGS))
then
echo "Usage: $0 dbname dbuser dbpass"
exit $E_BADARGS
fi
$MYSQL <<EOF
CREATE DATABASE IF NOT EXISTS '$1';
GRANT USAGE ON *.* TO '$2'#'localhost' IDENTIFIED BY '$3';
GRANT ALL PRIVILEGES ON '$1'.* TO '$2'#'localhost';
FLUSH PRIVILEGES;
EOF
}
# TODO tests on input args
deebee "$1" "$2" "$3"
And like Denis said in comments, take care of what users can put as arguments to prevent sql injection.
You should add some tests on input args.
The following code works on the command line
mysql --user='myusername' --password='mypassword' --database='mydatabase' --execute='DROP DATABASE myusername;
CREATE DATABASE mydatabase;'
However, it doesn't work on bash file on execution
#!/bin/bash
user=myusername
password=mypassword
database=mydatabase
mysql --user='$user' --password='$password' --database='$database' --execute='DROP DATABASE $user; CREATE DATABASE $database;'
I receive the following error:
ERROR 1045 (28000): Access denied for user '$user'#'localhost' (using password: YES)
How to make the bash file run as the command line?
Use double quotes while using BASH variables.
mysql --user="$user" --password="$password" --database="$database" --execute="DROP DATABASE $user; CREATE DATABASE $database;"
BASH doesn't expand variables in single quotes.
This one worked, double quotes when $user and $password are outside single quotes. Single quotes when inside a single quote statement.
mysql --user="$user" --password="$password" --database="$user" --execute='DROP DATABASE '$user'; CREATE DATABASE '$user';'
I have written a shell script which will read data from properties file and then run mysql script on shell script. sharing this may help to others.
#!/bin/bash
PROPERTY_FILE=filename.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
echo "# Reading property from $PROPERTY_FILE"
DB_USER=$(getProperty "db.username")
DB_PASS=$(getProperty "db.password")
ROOT_LOC=$(getProperty "root.location")
echo $DB_USER
echo $DB_PASS
echo $ROOT_LOC
echo "Writing on DB ... "
mysql -u$DB_USER -p$DB_PASS dbname<<EOFMYSQL
update tablename set tablename.value_ = "$ROOT_LOC" where tablename.name_="Root directory location";
EOFMYSQL
echo "Writing root location($ROOT_LOC) is done ... "
counter=`mysql -u${DB_USER} -p${DB_PASS} dbname -e "select count(*) from tablename where tablename.name_='Root directory location' and tablename.value_ = '$ROOT_LOC';" | grep -v "count"`;
if [ "$counter" = "1" ]
then
echo "ROOT location updated"
fi