Importing MySQL database from one server to another - linux

I have got two dedicated servers with root access. Both are running Linux. I want to import database from Server1 to Server2. I have already created an empty database on Server2.
I want to know Linux command through which I can import database directly? Is there such a feature? Can I use mysqldump? I want to avoid first taking database backup on server1, then moving that file to server2, and then importing that file.
Can import be done directly using some command?
Thanks

If you want to avoid creating a file, transferring it, and loading it, you can just pipe mysqldump into either an mysql running on the other server, or an ssh mysql on the other server.
Using mysql to connect to the remote box:
mysqldump --all-databases | mysql -h yourserver.com
Using ssh to connect to the other server
mysqldump --all-databases | ssh user#yourserver.com mysql
Use the mysqldump --all-databases to transfer them all, or just specify database names. Refer to the mysqldump documentation for more options.
You can also use the MySQL "Replication" feature, although that will take a bit more time to setup, and is rather tricky. Probably not worth all the time and trouble just for one single migration.

Stop mysqld on the first server, copy the data directory (usually /var/lib/mysql) from server 1 to server 2, start mysqld on the second server, and it will now be identical to the first.
You do not have to use the import/export tools if you can stop the server while you copy the data files. Especially if you can compress the files before copying them, this will be the fastest way.

mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user#newhost “mysql -uUSER -pPASS NEW_DB_NAME”
Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql
source

Related

Can't connect to database from snapshot. Ubuntu 18.04 postgres 9.5

I'm trying to connect to database restored from disk snapshot. I have new VM with ubuntu 18.04. I installed postgres, postgres-client and postgres-contrib from *.deb files. Then I mounted managed disk into machine with database and tried to connect - no success.
Postgres server running good, cluster ok, status ok, pg_isready ok. Until I change the data_directory in postgresql.conf from default to mounted - /snapshot_data_path/. After that my cluster is down and I cannot use psql in postgres (error ~ .s.PGSQL missing. File dissapearing from default directory).
chown -R to /snapshot_data_path - done, looks ok.
I think my database is running manualy when I use :
/usr/lib/postgresql/9.5/bin/postgres -d 3 -D /database/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf with that new path. EDIT: Error, I can connect but no data inside.
I try search over the internet for similar problems - no luck.
Is it possible to take database from 1 VM and connect it with another fresh VM? Or I must use pg_dump to achieve that?
Here the official documentation on this topic: https://www.postgresql.org/docs/9.0/backup-file.html
The database server must be shut down in order to get a usable backup.
Did you shutdown the server while the data snapshot was taken?

Move db2 from Windows to Linux

I'm trying to move db2 db from windows to linux server. When I move data to linux db by this command:
db2move DBNAME load -lo REPLACE -u userID -p password > load_remote.txt
I had this error:
SQLCODE: -3126 - SQLSTATE:
SQL3126N Remote client requires absolute path for files and directories.
Thanks.
Do you mean to use the 'load client' syntax (instead of just load) ?
See the details in the documentation.
The LOAD command requires that the files to be loaded are already on the Db2-target-server.
The LOAD CLIENT alternative allows the files to be on a remotely connected Db2-client (or on your Windows Db2-server if that is the source machine).
You can also just copy the IXF files to the Linux Db2-server, and open an SSH session to that Linux environment and run the LOAD command there. Your choice.
As with the LOAD command, LOAD CLIENT operates on one file at a time (in your case, one file per table) unless using lobsinsepfiles option, or other special cases.

Error when install postgresql in linux mint sarah

I want to install postgresql in my computer Linux mint sarah, and I finish procedure installation but when I type psql in my cmd postgres error, like this :
Error when install PostgreSQL
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
how I fixed that ??
thanks
The error states that the psql utility can't find the socket to connect to your database server.
Either you don't have the database service running in the background, or the socket is located elsewhere, or perhaps the pg_hba.conf needs to be fixed.
Step 1: Verify that the database is running
The command may vary depending on your operating system. But on most *ix systems the following would work, it will search for postgres among all running processes
ps -ef | grep postgres
On my system, mac osx, this spits out
501 408 1 0 2Jul15 ?? 0:21.63 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
The last column shows the command used to start the server, and the options.
You can look at all the options available to start the postgres server using the following.
man postgres
From there, you'd see that the options -D and -r are respectively the datadir & the logfilename.
Step 2: If the postgres service is running
Use find to search for the location of the socket, which should be somewhere in the /tmp
sudo find /tmp/ -name .s.PGSQL.5432
If postgres is running and accepting socket connections, the above should tell you the location of the socket. On my machine, it turned out to be:
/tmp/.s.PGSQL.5432
Then, try connecting via psql using this file's location explicitly, eg.
psql -h /tmp/ dbname
Step 3: If the service is running but you don't see a socket
If you can't find the socket, but see that the service is running, Verify that the pg_hba.conf file allows local sockets.
Browse to the datadir and you should find the pg_hba.conf file.
By default, near the bottom of the file you should see the following lines:
# "local" is for Unix domain socket connections only
local all all trust
If you don't see it, you can modify the file, and restart the postgres service.

Create PostgreSQL Database without root privilege

Currently, I use
$ sudo service postgresql start
to start the PostgreSQL server and
$ sudo -u postgres createdb testdb --owner ownername
to create a database. However, these commands need root privilege. How can I do these without root privilege/sudo on Linux (Ubuntu)?
You can run PostgreSQL without root privs by creating a new instance (which PostgreSQL calls a "cluster") and starting it.
You can't use the Ubuntu init scripts, wrapper tools like pg_ctlcluster, etc if you do this. You must use only PostgreSQL's own tools.
To create the new PostgreSQL instance with the superuser equal to your username, data directory in your home directory, and md5 auth enabled by default, use:
initdb -D $HOME/my_postgres -A md5 -U $USER
Adjust as desired; see initdb --help.
You'll then need to edit postgresql.conf to change the port to a non-default one, since your system probably runs its own postgres on the default port 5432. (If you want to limit access strictly to you, you can instead set listen_addresses = '' and unix_socket_directories = /home/myuser/postgres_socket or whatever. But it's simpler to just use a different port.)
To start it:
pg_ctl -D $HOME/my_postgres -w start
To connect to it, specify the port you chose:
psql -p 5434 ...
(If you changed unix_socket_directories you'll also want to specify the path you gave, like -h /home/myuser/postgres_socket.)
To make psql etc connect to your postgres by default, edit your ~/.bashrc to add something like
export PGPORT=5434
but note that'll also affect the default port for connections to other hosts.
To stop it:
pg_ctl -D $HOME/my_postgres -w stop
but you can also just shut down without stopping it, it doesn't care and will recover safely when you start it next.
To autostart it when you log in when it's set up in your home directory you'd have to use your desktop environment's run-at-startup features. They vary depending on environment and version so I can't give details here; it's different for GNOME 3, Unity (ubuntu), KDE, XFCE, etc.
Note that this approach still uses the system packages for PostgreSQL. This is important because if you uninstall (say) PostgreSQL 9.4 and install 9.6, your copy in your home dir will stop working. If you want it entirely independent of system packages, as you probably do if you don't control the system, you should compile PostgreSQL from source or use the binary installer to install inside your home directory.
Postgres can run without root permission.
Just download from
https://www.enterprisedb.com/download-postgresql-binaries
and run
Init database
./initdb -D /data
Run postgres
./bin/postgres -D /data
Create database
./bin/createdb mydb
Connect with psql
./bin/psql mydb
(https://www.golery.com/pencil/vU)

Using rsync to keep two servers in sync

I have two AWS EC2 instances that I'm trying to implement a two way sync. So if a file or folder on server1 is created or updated it should sync that file/folder to server2. If it's a new folder it should be created on the server. The problem I'm having is I can't get rsync to create the folders on the 'local' server.
For example, server 1: /rootdir/1/2/3/4, where directories 3 and 4 do not exist on server2. When I run rsync on server2 I want those new directories to be created.
Here is the code I'm trying to use, running from Server2:
$sudo rsync -avzP -e "ssh -i /home/ec2-user/.ssh/Key.pem" ec2-user#IPADDRESS OF SERVER1:/rootdir/1/2/ /rootdir/1/2
I'm not getting an error but the directories aren't being copied.
I also tried -r but it made no difference.
I finally figured out what I was doing wrong. The servers were configured with a non-standard port and I needed to tell rsync which port to use.

Resources