Is there a way to test a connection to a Postgres DB via command line (bash) without using external tools, such as psql or pg_isready? Trying to write a bash script.
Related
I am running the bash files to make a Mongo dump on daily bases.But In local directory I am running a one bash file which connects to server terminal.And in server terminal I am running the other file which makes a Mongo dump.
But is it possible to make one file which connects to MongoDB server terminal and run the commands on the sever.
I tried with many commands but it was not possible to run the commands on the server terminal with one bash file, when the server terminal opens up then the left over commands does not execute.
Is it possible to do one bash file and execute the server commands on the server..?
Connect to your DB remotely using this command :
mongo --username username --password secretstuff --host YOURSERVERIP --port 28015
You can then automate this by including your pertaining commands ( including the above ) in a bash script that you can run from anywhere.
To solve the above problem, answer from Matias Barrios seems to be correct for me. You don't use a script on the server, but use tools on your local machine that connect to the server services and manage them.
Nevertheless, to execute a script on a distant server, you could use ssh. This is not the right solution in your case, but answer the question in your title.
ssh myuser#MongoServer ./script.sh param1
This can be used in a local script and execute script.sh on the server MongoServer (with param1 and) with system privileges of the user myuser.
Beforehand, don't forget to avoid password request with
ssh-copy-id myuser#MongoServer
This will copy your ssh public key in the myuser directory of the MongoServer
I have run into a problem with the psql command in my BASH script as I am trying to login to my local postgres database and submit a query. I am using the command in the following way:
psql -U postgres -d rebasoft_appauditor -c "SELECT * FROM katan_scripts"
However, I get the following error message.
psql: FATAL: Ident authentication failed for user "postgres"
This runs perfectly fine from the command line after I appended the following changes to /var/lib/pgsql/data/pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 trust
Also, could this please be verified for correctness?
I find it rather strange that database authentication works fine on the command line but in a script it fails. Could anyone please help with this?
Note: I am using MAC OSX
It might possibly depend on your bash script.
Watch for the asterisk (*) not be replaced with the file names in your current directory. And possibly a semicolon or \g might help to actually send the SQL statement to the database server.
I can connect to a mongo server using this command:
mongo host:port/admin --username=user --password=pass
and I get a mongo shell where I can execute any type of mongo commands after
I switch to the dev db (use dev).
I would like to execute a mongo command from the unix shell / command line
on the dev database, but I have access only to admin.
How can I do that? How can I specify the db I would like to use?
If you wish to execute just a single command, you could do it with:
mongo server:22021/my_dbname --eval "printjson(db.serverStatus())"
If you wish to execute multiple commands say residing in a file called command.js, you could do something like:
mongo server:22021/my_dbname --quiet commands.js
If you want pure shell script (SQL) style, then you could do something like:
function testMongoScript {
mongo server:22021/my_dbname <<EOF
use mydb
db.leads.findOne()
db.leads.find().count()
EOF
}
For further details, you could refer here as well.
You can use output redirection to run whatever command you need:
mongo localhost:27017 <<< $'rs.slaveOk()\nshow dbs'
or
echo $'rs.slaveOk()\nshow dbs' | mongo localhost:27017
just remember the \n between commands.
Use --authenticationDatabase to authenticate to the database where your user exists even though you are connecting to a different database:
mongo localhost:27017/dev -u user -p **** --authenticationDatabase admin
Naturally, you won't be able to do anything in the dev database your user doesn't have permissions to do.
I am just getting started with shell scripts to save me typing in the same commands over and over. This command is used to copy a database over to a slave server as part of setting up MySQL database replication.
It works when typed into the command prompt directly:
mysqldump --host=192.168.1.1 –uUSER –pPASSWORD --opt database_name | mysql --host=192.168.1.2 –uUSER –pPASSWORD -C database_name
USER, PASSWORD and database_name all are replaced with their actual values in the real script.
When I type this command into a scripts.sh file, give it the execute permission, and then run it with ./scripts.sh I get:
'RROR1102 (42000): Incorrect database name 'database_name
mysqldump: Got errno 32 on write
What could be causing this error? Do I need to modify the command somehow when it is contained in a shell script?
The variable your database name is in has a CR at the end. You may need to run your script through dos2unix, or use one of the solutions on this site for stripping CRs from data if you're getting the database name from an external source.
Is there any way I can run Sybase SQL commands from command prompt. I need to write a batch file which runs an SQL query on machine as a fix for a bug.
Use ISQL. See http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.utility/html/utility/utility10.htm for the complete reference.
Specifically, you're looking for commands like this:
ISQL -S server -D database -U user -P password
This will launch ISQL with the indicated settings, and from there you can run SQL statements against the database.