Running a single mysql command with the 'runuser' command - mysql

For some strange reason, I can't find a way to make the runuser command work. I know it is possible to achieve this with sudo -u mysql mysql -e "$DB_SETUP but since I want to do this inside a script that already runs with sudo I find this not very pretty.
Here is what I am trying to do:
DB_SETUP="CREATE USER IF NOT EXISTS $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';CREATE DATABASE IF NOT EXISTS $DB_NAME;GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';FLUSH PRIVILEGES;"
sudo runuser -u mysql "mysql -e \"$DB_SETUP\"" # does not work
It gives me this error:
runuser: failed to execute mysql -e "CREATE USER IF NOT EXISTS db_user#baseurl IDENTIFIED BY 'db_password';CREATE DATABASE IF NOT EXISTS db_name;GRANT ALL PRIVILEGES ON db_name.* TO db_user#baseurl IDENTIFIED BY 'password';": No such file or directory

As commented above, I got it working with:
sudo runuser -u mysql mysql <<< $DB_SETUP
No quotation marks at all!

Related

How to run custom mysql script for Laravel app in test pipeline in CodeShip Basic?

Relative newbie to PhpUnit and testing in general. We do not use migrations in our project, but have a couple of scripts that I need to run in order to set up the database for testing. How can I run mysql scripts from the project in the test pipeline? I also need to create a new database with a specific name before running those scripts.
Thanks!
The commands that you use on your local machine are the same commands you can run in CodeShip Basic. CodeShip Basic is just a build machine with Ubuntu Bionic and it will run through the setup, test, and deploy commands as if you were entering each line into your CLI. :)
We also have some documentation about mysql: https://documentation.codeship.com/basic/databases/mysql/
OK, after some digging I found out how to do all this. Below is the script I used for testing with a new mysql schema created with specific user from a sql script. Hope this helps someone in the future.
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE USER 'myuser'#'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE SCHEMA myschemaname;"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'#'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/1of2-schema.sql
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/2of2-seed.sql
php artisan passport:install
./vendor/bin/phpunit
$MYSQL_USER and $MYSQL_PASSWORD are replaced by Codeship - these are the env variables for the user and password for the mysql that exists in the build container.
the -e switch on the mysql call runs the script given and exits. Had to do it this way since I couldn't interact with the mysql client.

How to disable dot replacement in bash?

I want to run MySQL query from bash script to create new user:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass'"
Where $BUILD variable is set to a number, for example 15, so final query should look like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.* TO appuser#localhost IDENTIFIED BY 'somepass'"
But... in directory where script is and is run there is a tar archive with same name as database, so query becomes like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.tar.gz TO appuser#localhost IDENTIFIED BY 'somepass'"
I guess this happens because there is a match, but changing file name is not an option. Moving script to other directory is not allowed too.
I tried to escape dot with backslashes (appdb_${BUILD}\.*) - nothing changed.
How I can fix this?
There is nothing wrong with your approach and it is working in my case. See below. Can you tell us what is the error that you are getting so that we can help you?
[root#cloud mysql]# ls
appdb_15.tar.gz test.sh
[root#cloud mysql]# cat test.sh
#!/bin/bash
BUILD=15
# First Method
mysql -u root -ptest -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass';"
# Second Method
mysql -u root -ptest <<EOF
GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser1#localhost IDENTIFIED BY 'somepass';
select user, host from mysql.user where user like 'appuser%';
EOF
[root#cloud mysql]# ./test.sh
user host
appuser localhost
appuser1 localhost
EDIT 1:
I forgot to mention that this is definitely not a bash issue.
EDIT 2: Changed the build variable to 15 from 5.

MySql 1045 When using --defaults-file

I am having a strange issue. I have MySql running on RHEL. I can logon to MySql with
mysql -uroot -pmyPassword
and it works fine. Also, when I try to execute a query from a .sh script as below it works fine
mysql --user=root --password=myPassword --host=localhost --port=3306 -se "SELECT 1 as testConnect" 2>&1>> $OUTPUT
But when I store the userid and password in a msql.conf file as below
[clientroot]
user=root
password=myPassword
and then change the line in the script as below
mysql --defaults-file=msql.conf --defaults-group-suffix=root -hlocalhost -P3306 -se "SELECT 1 as testConnect" 2>&1>> $OUTPUT
When I run it, I get the error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I am running the script with sudo and the config file is at the same directory as the script
I have permission 0600 on the config file.
How do I make this work?
It worked for me, but it was a bit of a 'tricky' fix that isn't shown in the actual documentation. All you have to do is change
[clientroot]
user=root
password=myPassword
to
[clientroot]
user="root"
password="myPassword"
Basically just add the double quotes.
Then running your command:
mysql --defaults-file=msql.conf --defaults-group-suffix=root -hlocalhost -P3306 -se
"SELECT 1 as testConnect" 2>&1>> $OUTPUT
Should work (it worked for me).
I discovered this by looking through an obscure part of the documentation on something almost unrelated, so I don't blame everyone for missing it.
Options files are not meant for auto-login credentials.
Try this:
export MYSQL_PWD=myPassword
And try to connect using the root user.
Note that this approach is "insecure", but so is the basic idea of what you're trying to do.
When your goal is to not use the password in scripts or on command line, may I suggest a different approach?
You can create a file with the login credentials encrypted and use that for logging in.
Here's an example:
Create the file with
mysql_config_editor --set-login-path=local --host=localhost --user=localuser --password
This creates the file ~/.mylogin.cnf. You can now login with
mysql --login-path=local
local is a name you can specify, btw.
Here's another plus: You can also create different files for different projects. For example, I sometimes create files for certain scripts. I put the .mylogin.cnf in the same folder as the script and use it like this in the script:
RESULT=($(MYSQL_TEST_LOGIN_FILE=./.mylogin.cnf mysql --login-path=$HOST -B --skip-column-names -e "SELECT whatever FROM whatever"))
Read more about mysql_config_editor here.

How can I insert MySQL in a bash script

I would like to insert MySQL into my bash script.
How can I insert the following lines into bash code?
This is how I would enter MySQL...
mysql -uroot -p
and use this MySQL code...
GRANT SELECT, INSERT, UPDATE, DELETE
ON example.*
TO 'yourUser'#'localhost'
IDENTIFIED BY 'yourUserPassword';
\q
You don't need to use bash to do this, simply use the -e flag:
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
Notice that you might have to flush the privileges after running the above:
FLUSH PRIVILEGES;
From the documentation:
Reloads the privileges from the grant tables in the mysql database
To do this from a bash script you create a sh file with the following content:
#!/bin/bash
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
mysql -uroot -p<password> -e"FLUSH PRIVILEGES"
Don't forget that you must set execute permission on the file:
chmod +x <filename>.sh
Then you should be able to execute the script using:
./<filename>.sh

Create MySQL DB from Command Line

I am trying to create a batch file to create a MySQL Database. So far, none of the information I am finding is working. Just for testing, this is what I am trying...
C:\>mysql -uroot -ppassword < CREATE DATABASE testdb;
C:\>mysql -uroot -ppassword mysql < CREATE DATABASE testdb;
No matter how I put it, I keep getting the error "The system cannot find the file specified". If I just put...
C:\>mysql -uroot -ppassword
It logs into the MySQL prompt fine. What exactly am I doing wrong?
I agree with the other posters, it's much better to put the schema into a file. However, here's how you can do it on the command line:
mysql -uroot -ppassword -e "CREATE DATABASE testdb"
acess as root user :
mysql -u root -p
it asks for password..enter your password
then
run the create command like this:
mysql> create database database_name;
It's better to write your MySQL inside a file and then import that file. That way, when you need to run it again (reinstalling or migrating) you have a record of the MySQL to run. The code I use for a file like this is as follows, which destroys anything that's already there, and then creates the database and assigns a dedicated user.
# uninstall if anything's already there
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%';
DROP USER 'username'#'%';
DROP DATABASE IF EXISTS `tablename`;
# create the user
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `tablename`;
GRANT ALL PRIVILEGES ON `tablename` . * TO 'username'#'%';
Try putting your sql into a text file, like 'createDb.sql' and then run:
mysql -uroot -ppassword < createDb.sql;