I've been searching to understanding the following MySQL command:
framework_mariadb.sql | mysql -u username -p password -h 127.0.0.1 -P 26257 target
My guess was the sql statements within the sql file get executed by mysql for the given target/database. But then I came across the source command in MySQL, ie
\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
So my question is, does the first command and the second command essentially do the same thing? My apologies if this has already been asked, I haven't been able to find details for the first SQL command.
This is more about Linux shell capabilities than it is about MySQL.
The second form runs the mysql client, and uses the < symbol to tell it to take its input from the specified file.
The first form does essentially the same thing, but uses the pipe character | to indicate that the output of the first command should be sent to the input of the second command.
However, for the first form I'd expect the line to start with cat (as in cat framework_mariadb.sql | mysql ...) because the SQL script won't normally run as a shell command. cat is a command that reads one or more files and send s them to its output.
It is possible to set the SQL script up to run like this, but that requires a specific line (#! /bin/cat or similar) to be present at the top of the file, and the file must have the execution bit set. (At least, that's how I'd do it. There might be some other bash magic I'm not aware of. bash is not my forté)
There are many resources on the web that can teach the fundamentals of the Linux shell. You could try Microsoft's Introduction to bash, but there are many others.
Related
How can I execute multiple SQL queries in the bash script?
I read these two posts from previous years:
A better way to execute multiple MySQL commands using shell script
How to execute a MySQL command from a shell script?
They brought some clarification, but there is still something I do not understand.
I have multiple queries for deleting information about subject with defined subject_id.
Unfortunately I need to run all of them since the table is not in the "cascade" mode.
Is there a way, to create a bash script in which I can use the "user given" variable (by that I mean for example [ read -p 'Subject ID' SUBJECT_ID ]) that will be used inside as the subject_id in each of the queries?
Do I still have to adjust everything to this:
mysql -h "server-name" -u root "password" "database-name" < "filename.sql"
or is there a way to just run this script with connection to db from .cnf file inside it?
There are two questions above. One is how to get a bash variable into your SQL script. I would do this:
read -p 'Subject ID' SUBJECT_ID
mysql -e "SET #subject = '${SUBJECT_ID}'; source filename.sql;"
Bash will expand ${SUBJECT_ID} into the string before it uses it as an argument to the mysql -e command. So the MySQL variable is assigned the string value of SUBJECT_ID.
This will be tricky if SUBJECT_ID may contain literal single-quote characters! So I suggest using Bash syntax for string replacement to make each single-quote in that into two single-quotes:
mysql -e "SET #subject = '${SUBJECT_ID//'/''}'; source filename.sql;"
Note you must put a semicolon at the end after the filename.
The second question is about specifying the host, user, and password. I would recommend putting these into an options file:
[client]
host=server-name
user=root
password=xyzzy
Then when you invoke the mysql client:
mysql --defaults-extra-file myoptions.cnf -e '...'
This is a good idea to avoid putting your plaintext password on the command-line.
Read https://dev.mysql.com/doc/refman/8.0/en/option-files.html for more details on option files.
I'm trying to run a mysql query on a remote host using a bash script.
${MYSQL} -u ${USER} -p${PASS} -P${PORT} -h ${HOST} -e "select * from information_schema;"
My PASS looks something like "dfsf#DFD". It conatins '#' character.
For some reasons the PASS is not retrieving correctly in the script. Its getting chopped off after '#'.
And for some reason including source /etc/environment in the script, seems to fix the problem.
I want to understand why is this happening. How to make it work without the source statement.
As far as I know - the safest way to specify remote connection details for MySQL is using configuration file: https://dev.mysql.com/doc/refman/8.0/en/option-files.html
So briefly you just put something like this in ~/.my.cnf file:
[client]
host='...'
port='...'
user='...'
password='...'
And then you simply run
mysql -e "select * from information_schema;"
Not sure it suits your situation, but it should solve your issue, if you quote the values.
Quoting at command line is also a solution, but your credentials will be visible to everyone able to see your processes at the system where you run that.
I've been trying for a few hours now to store row data into a BASH var then use that to launch some PHP scripts.
So far, I've only been able to echo out the whole result set. I have since broken that script, so I can't even paste it here. This is what I have, that's not working at all.
#! bin/bash
query=`echo "SELECT id FROM searches WHERE running=1 AND id_user=2" | mysql -u root`
I've never used BASH before, so I'm completely lost.
What I'm asking for is some resources that can show me how I can connect to mysql, then loop through a result set using the data from that row.
Thanks.
You can read mysql --silent output into Bash vars with while read:
>sql="SELECT id FROM searches WHERE running=1 AND id_user=2"
>mysql --silent -u ctrahey test -e "$sql" |while read myid; do
php -f my_processor.php $myid;
done
>
Notes:
sql="... sets a Bash variable of your sql (not actually necessary, just aides readability)
mysql -e the -e option allows you to pass in a query, so you don't need STDIN
mysql --silent --silent suppresses the extra formatting mysql usually does. in my testing, this was actually also unnecessary (the pipe chars in the output did not mess up Bash)
php -f .. this executes a php file, passing the current id as an arg.
Bash is not a good language for interacting with mysql, it only works for very very simple cases. Use php instead (since you mentioned php), it has an api for interacting with mysql databases sanely. And yes, you can run a php script as you would a bash script.
If you want to run a single command with MySQL, use mysql -e or mysql --execute. They are the same command, but the second version is more memorable. If you add this flag to the above, the rows will be stored in the variable.
However, as mentioned by geirha, BASH may not be the best language to get row data. But if you choose to parse it wish BASH, mysql -e is the right command to execute queries.
A mysqldump command like the following:
mysqldump -u<username> -p<password> -h<remote_db_host> -T<target_directory> <db_name> --fields-terminated-by=,
will write out two files for each table (one is the schema, the other is CSV table data). To get CSV output you must specify a target directory (with -T). When -T is passed to mysqldump, it writes the data to the filesystem of the server where mysqld is running - NOT the system where the command is issued.
Is there an easy way to dump CSV files from a remote system ?
Note: I am familiar with using a simple mysqldump and handling the STDOUT output, but I don't know of a way to get CSV table data that way without doing some substantial parsing. In this case I will use the -X option and dump xml.
mysql -h remote_host -e "SELECT * FROM my_schema.my_table" --batch --silent > my_file.csv
I want to add to codeman's answer. It worked but needed about 30 minutes of tweaking for my needs.
My webserver uses centos 6/cpanel and the flags and sequence which codeman used above did not work for me and I had to rearrange and use different flags, etc.
Also, I used this for a local file dump, its not just useful for remote DBs, because I had too many issues with selinux and mysql user permissions for SELECT INTO OUTFILE commands, etc.
What worked on my Centos+Cpanel Server
mysql -B -s -uUSERNAME -pPASSWORD < query.sql > /path/to/myfile.txt
Caveats
No Column Names
I cant get column names to appear at the top. I tried adding the flag:
--column-names
but it made no difference. I am still stuck on this one. I currently add it to the file after processing.
Selecting a Database
For some reason, I couldn't include the database name in the commandline. I tried with
-D databasename
in the commandline but I kept getting permission errors, so I ended using the following the top of my query.sql:
USE database_name;
On many systems, MySQL runs as a distinct user (such as user "mysql") and your mysqldump will fail if the MySQL user does not have write permissions in the dump directory - it doesn't matter what your own write permissions are in that directory. Changing your directory (at least temporarily) to world-writable (777) will often fix your export problem.
In linux I can use ! to start a shell command:
mysql >\! ping localhost
Is there a way to get the output of shell in MySQL?
While in the mysql prompt, no. You can however pipe the results of a command into mysql with something similar to:
mysql -u user -ppassword < `ping localhost`
The only issue with this is that whatever your shell command returns needs to be the sum of the commands/data you want to issue to mysql. This is most commonly used to execute a script or something similar.