make neo4j commands from terminal or shell script - mysql

Is there any way to make commands to neo4j from the command line without entering the shell, in other words the equivalent of:
mysql -u user -ppassword -e "INSERT INTO something VALUES something-else"
I'd also like to be able to make commands from an executable script, something akin to:
mysql -u user -ppassword << EOF
INSERT INTO something VALUES something-else;
EOF
Does anyone know the best way to do this?

You could always use curl to post requests to the REST API.
curl --header "Content-Type: application/json" \
--request POST \
--data '{"statements" : [ { "statement" : "create (n:Node {name: \"Test\"} ) return n" } ] }' \
http://localhost:7474/db/data/transaction/commit
Here is the Neo4j REST API reference.

You can pass arguments to the neo4j-shell command :
Execute cypher passed in the command :
./bin/neo4j-shell -c 'match(n:Page) RETURN count(n);'
Or execute a cypher script file :
./bin/neo4j-shell --file your-file.cql

Related

Dialog and SQL output on Linux Shell Scripting

I have the following script:
VAR=$(mysql -u root -e " use <database_name>; select column1,column2,column3 from <table_name>;")
dialog --title "something" --msgbox "$VAR" 50 50
and the output that I got is like this.
this
but i'm expecting the output to be like this instead inside the dialog command
this
I can get this output by not storing the sql commands in a variable.
where am I wrong here?
On your VAR=$(mysql ...), you can add --table to get the disired effect.
VAR=$(mysql -u root -t -e ...)

Expand selected variables within here-document while invoking Bash through SSH

I don't have remote access to a MySQL server, so I am trying to do it via an SSH session.
It partly works, but not correctly.
sshpass -p $password ssh user#$IP /bin/bash << EOF
mysql -N -uroot -ppassword test -e "select id from client where user ='$user'"
EOF
This will show the result of the select statement, but I'd like to be able to use that result in another echo statement.
eg:
The user ID is: xxxxx
I tried to assign the output to a variable using:
sshpass -p $password ssh user#$IP /bin/bash << EOF
res=$(mysql -N -uroot -ppassword test -e "select id from client where user ='$user'")
echo $res
EOF
But that results in:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysql/mysql.sock' (2)
If I quote EOF like 'EOF' then I can get the result into res but I lose the ability to use $user
Is there anyway to do this so I can use my variable in the heredoc and get the result of the MySQL query to a new variable ?
If I quote EOF like 'EOF' then I can get the result in to res but I lose the ability to use $user
You can pass $user to bash as a positional parameter and still have the quoted EOF and its advantages. E.g:
sshpass -p "$password" ssh "user#$IP" /bin/bash -s "$user" << 'EOF'
res=$(mysql -N -uroot -ppassword test -e "select id from client where user ='$1'")
echo $res
EOF
Bash manual describes the -s option as follows.
If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell or when reading input through a pipe.

Why is this bash variable blank when taking output from mysql?

I am trying to take the output from a MySQL query in bash and use it in a bash variable, but it keeps coming up blank when used in the script, but works perfectly from the terminal. What's wrong here?
I've tried changing the way the statement is written and changing the name of the variable just in case it was somehow reserved. I've also done a significant amount of searching but it turns out if you but 'bash', 'blank', and 'variable' in the search it usually comes up with some version of how to test for blank variables which I already know how to do.
tempo=$(mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;")
printf "the output should be: $tempo" # This is a test statement
The end result should be that the $tempo variable should either contain a user name from the database or be blank if there isn't one.
I think there is some error with your sql statement at user = '$temp' > 0.
But to get the result from MySql you have to redirect the standard error (stderr) to the standard output (stdout), you should use 2>&1.
Most probably you will run into MySql error but try running this on terminal.
tempo=$((mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;") 2>&1)
The solution was to echo the result of the sql query like this:
tempo=$(echo $(mysql -u "$dbuser" -p"$dbpass" -D "$database" -s -N -B -e "select user from example where user='$username' > 0;"))
Now I'm left with logic issues but I think I can handle that.

connect to mysql db and execute query and export result to variable - bash script

I want to connect to mysql databse and execute some queries and export its result to a varibale, and do all of these need to be done entirely by bash script
I have a snippet code but does not work.
#!/bin/bash
BASEDIR=$(dirname $0)
cd $BASEDIR
mysqlUser=n_userdb
mysqlPass=d2FVR0NA3
mysqlDb=n_datadb
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1")
echo "${result}" >> a.txt
whats the problem ?
The issue was resolved in the chat by using the correct password.
If you further want to get only the data, use mysql with -NB (or --skip-column-names and --batch).
Also, the script needs to quote the variable expansions, or there will be issues with usernames/passwords containing characters that are special to the shell. Additionally, uppercase variable names are usually reserved for system variables.
#!/bin/sh
basedir=$(dirname "$0")
mysqlUser='n_userdb'
mysqlPass='d2FVR0NA3'
mysqlDb='n_datadb'
cd "$basedir" &&
mysql -NB -u "$mysqlUser" -p"$mysqlPass" -D "$mysqlDb" \
-e 'select * from confs limit 1' >a.txt 2>a-err.txt
Ideally though, you'd use a my.cnf file to configure the username and password.
See e.g.
MySQL Utilities - ~/.my.cnf option file
mysql .my.cnf not reading credentials properly?
Do this:
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1" | grep '^\|' | tail -1)
The $() statement of Bash has trouble handling variables which contain multiple lines so the above hack greps only the interesting part: the data

mysql command line generate 2 files from 1 source

If I would like to generate 2 files(same data,same file name but different path) How can I do this?
Here is my code
mysql -h -u -p -e "select * from customer" prdb> D:\patha.txt
mysql -h -u -p -e "select * from customer" prdb> C:\patha.txt
Please suggest if there is a better way to do.
This seems like a use case for tee if you have Powershell (you're on Windows I suppose) available. It works like this:
echo "query result" | tee fileC fileD
For the docs of tee in Powershell, see here.