Install, backup and restore Neo4j database
Contents
Tested on: Ubuntu 22.04
Before install you have to check your
OpenJDK version, for example forneo4j 5.8.0you should haveopenjdk 17.0.7 2023-04-18.
Installation:
Get gpg.key and add neo4j repository:
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/neo4j.gpg
echo "deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable latest" | sudo tee -a /etc/apt/sources.list.d/neo4j.list
sudo apt updateInstall needed java version:
# for neo4j 5.8.0
sudo apt install openjdk-17-jre
java --versionIf you have several versions of java set needed version:
sudo update-alternatives --config javaAnd, finally, install neo4j and cypher-shell:
sudo apt install neo4j cypher-shell -yChange these lines in /etc/neo4j/neo4j.conf
server.default_listen_address=0.0.0.0
# Bolt connector
server.bolt.enabled=true
server.bolt.tls_level=DISABLED
server.bolt.listen_address=:7687
#server.bolt.advertised_address=:7687
# HTTP Connector. There can be zero or one HTTP connectors.
server.http.enabled=true
server.http.listen_address=:7474
#server.http.advertised_address=:7474Set password and start neo4j:
sudo -u neo4j neo4j-admin dbms set-initial-password your_password
sudo systemctl enable --now neo4jDon’t forget open ports in your firewall! Recover a password if you forgot it
Some activities with neo4j database
neo4j-adminshould fromneo4juser Fix if you broke permissions:sudo chown -R neo4j:neo4j /var/lib/neo4j
Connect:
cypher-shell -a ubu22.home:7687 -u neo4j -p your_passwordhttp link: http://ubu22.home:7474/browser/
Show databases and create data:
Helpful information from digitalocean.com
show databases;
show database neo4j;
SHOW DATABASE neo4j YIELD *;
CREATE (:Shark {name: 'Great White'});
CREATE
(:Shark {name: 'Hammerhead'})-[:FRIEND]->
(:Shark {name: 'Sammy'})-[:FRIEND]->
(:Shark {name: 'Megalodon'});
MATCH (a:Shark),(b:Shark)
WHERE a.name = 'Sammy' AND b.name = 'Megalodon'
CREATE (a)-[r:ORDER { name: 'Lamniformes' }]->(b)
RETURN type(r), r.name;
MATCH (a:Shark),(b:Shark)
WHERE a.name = 'Sammy' AND b.name = 'Hammerhead'
CREATE (a)-[r:SUPERORDER { name: 'Selachimorpha'}]->(b)
RETURN type(r), r.name;
MATCH (a)-[r]->(b)
RETURN a.name,r,b.name
ORDER BY r;Backup neo4j database
sudo mkdir -pv /var/backups/neo4jbackups && sudo chown neo4j:neo4j /var/backups/neo4jbackups
sudo systemctl stop neo4j
sudo -u neo4j bash
neo4j-admin database check neo4j
neo4j-admin database dump --to-path=/var/backups/neo4jbackups neo4j
2023-05-19 07:11:08.400+0000 INFO [o.n.c.d.DumpCommand] Starting dump of database 'neo4j'
Done: 36 files, 257.9MiB processed.
2023-05-19 07:11:08.977+0000 INFO [o.n.c.d.DumpCommand] Dump completed successfully
exit
sudo systemctl start neo4jIn a neo4j docker installation, the neo4j-admin restore requires the neo4j service to be stopped as one can’t do a restore on a running database. More information is here.
Restore neo4j database
sudo systemctl stop neo4j
sudo -u neo4j bash
neo4j-admin database load --from-path=/var/backups/neo4jbackups --overwrite-destination=true neo4j
Done: 36 files, 257.9MiB processed.
neo4j-admin database check neo4j
exit
sudo systemctl start neo4j