Run psql query using ssh - ssh

I wrote a file.sh to execute psql query from remote sever. I'm stuck on the first step:
ssh user#myserver.com "psql database"
=> Nothing happens
I tried several different syntaxes that I found on google, but nothing works for me.
Manualy I process like this without problem:
$ ssh myserver
$ psql database
database=> select foo from table;
I don't know what I missed (it's my first bash script)

I use SSH every day to connect to my work server and use ssh user#server. From there, I am able to run files, connect to my database, etc.

Related

How does running ssh with a command string work behind the scene?

I have some difficulities on understanding how ssh works behind the scene when I run it with a command string.
Normally, I type ssh rick#1.2.3.4 then I am logged into the remote machine and run some commands. If I don't use nohup or disown, once I close the session, all running processes started by ssh will stop. That's the ordinary case.
But if I run ssh with a command string, things become different. The process started by ssh won't stop if I close the session.
For example:
Run from local command line: ssh rick#1.2.3.4 "while true; do echo \"123test\" >> sshtest.txt"; done
Run a remote script ssh rick#1.2.3.4 './remoteScript_whichDoTheSameAsAbove.sh'
After closing the session by Ctrl + C or kill pid on the local machine, on the remote machine I see the process still running with ps -ef .
So my question is, could someone make a short introduction on how ssh works when I run it with a command string like above?
Also, I get very confused when I see these 2 related questions during searching:
Q1: Getting ssh to execute a command in the background on target machine . What is this question asking for? Isn't ssh rick#1.2.3.4 'some command' already run as a seperate shell/pts? I don't understand what "in the background" is for.
Q2: Keep processes running after SSH session disconnects Isn't simply running a remote script meets his requirement? ssh rick#1.2.3.4 "./remoteScript.sh. I get very confused when I see so many "magic" answers under that question.

Connect to sqlplus in shell script and run sql script with separate password

I am writing a shell script in Jenkins that logs into a database and runs an sql file. All of the commands are logged to the console, so if I use the simple login method for sqlplus (sqlplus -s $USERNAME/$PASSWORD#connectionstring), the password gets logged, which isn't ideal.
This works:
sqlplus -S ${USERNAME}/${PASSWORD}#connectionstring #sql_update.sql
but the logging on Jenkins shows the command once the values have been substituted:
+ sqlplus user123/pass123#connectionstring #sql_update.sql
To avoid having the password logged, I am trying to use the sqlplus login method where you just provide the username and then get asked to input the password.
I have tried the following, but I am getting ORA-01-17: invalid username/password; logon denied
sqlplus -s ${USERNAME}#\"connectionstring\" <<EOF
${PASSWORD}
sql_update.sql
exit
EOF
Is there something obviously wrong with this?
It's worth noting that simply disabling the console logging isn't an option, as we need it for other things in the script.
Also, the difference between this question and Connect to sqlplus in a shell script and run SQL scripts is that I am asking about providing the password separately.
EDIT I managed to partially resolve the issue thanks to Echo off in Jenkins Console Output. My script now contains set +xbefore the commands are run, which hides the commands. However, I'd still like to know what was wrong with the above, so am leaving this question open for now.

why ssh-copy-id still need password when using fabric

I have set env.user and env.password,but when I use:
run('ssh-copy-id -i $HOME/.ssh/id_rsa.pub server1')
it still asks me for the password,why?
env.user and env.password are used to ssh to remote server and then run the command inside run () at the remote server.
They are not used for the remote command itself (running in the remote server).
So ssh-copy-id doesn't know anything about the user or password.
If this is not what you mean, I would advice you to edit the question and make things more clear, like providing more context of what you are trying to accomplish: pasting the function that contains this 'run' for example.

Extract mysql Database from CommandLine

I am using wamp server but something happend to it and now there is no way to make it come back to life. I only have access to mysql command line, and i have some important databases there.
How can i export the database using commandline?
please help.
mysqldump -h localhost --routines -u root -p dbname
If the mysql server is not behaving normally, you might have some corrupted tables. It happens usually after an unscheduled restart. mysqldump won't be able to export corrupted tables.
If the mysql server is running, try connecting to it using phpmyadmin and use the built in tools to fix and check the tables
if the mysql server fails to start, check the log files and use the mysql command line tools to fix the issues
myisamchk --silent --force --fast --update-state /var/lib/mysql/dbname/*.MYI
(http://www.thegeekstuff.com/2008/09/how-to-repair-corrupted-mysql-tables-using-myisamchk/)
First try to connect to Mysql server using the command line
mysql -uUsername -p and press enter.This will prompt for password.Enter the mysql server password.
Then use the mysqlcheck command to repair the server in case corruption has occured
COmmand for mysql-check : mysqlcheck --repair --use-frm --all-databases
I think you can just copy the folders of the database to create a backup of MySQL databases.
Used to work.

Stop (long) running SQL query in PostgreSQL when session or requests no longer exist?

I'm not sure where to start on addressing this issue but if I have a AJAX web application that sends requests to the server and runs long queries on the database (postgresql in my case), is there a way to stop or kill the queries if while still running, the user refreshes the page or closes the session...etc?
To stop the query:
SELECT pg_cancel_backend(procpid);
To kill the database connection:
SELECT pg_terminate_backend(procpid);
To get an overview of the current transactions, to get the proced id's:
SELECT * FROM pg_stat_activity;
http://www.postgresql.org/docs/current/interactive/functions-admin.html
Considering your psql configured to run and connect to current dev database this one liner comes handy in development when testing complicated queries which can hung, just kills whatever runs:
If not configured properly - add flags for user/password/database
psql -c "SELECT procpid FROM pg_stat_activity;" -t | xargs -n1 -I {} psql -c "SELECT pg_cancel_backend({})"