How to delete mysql db rows remotely? - mysql

Can I run mysql delete row remotely from other machine?
Something alike mysqldump which run locally dumping data on other remote machine.

try:
mysql -u yourdblogin -pyourdbpassword -h yourdbdomain.yourdomain.com yourdb -e "delete from table where x = y"
not sure if -e is the correct argument, but the though is sound.

Only if you have TCP connections configured on the MySQL server, and you have a username and password configured that will allow you to remotely connect.
If you check the mysqldump documentation, you'll find the -host option allows you to connect to a remote system.

You can try something like this:
mysql -u yourdblogin -pyourdbpassword -h yourdbdomain.yourdomain.com yourdb
Sorry if I misunderstood your question.

The specific way of doing this is to connect to the remote machine using the mysql prompt and then execute queries against it (queries which cause a row to be deleted.)
# mysql -u username -p -h remote.location.com
mysql> USE `database_name`;
mysql> DELETE FROM `table` WHERE id=1234;
The DELETE FROM syntax is SQL syntax, and the WHERE part of it allows you to specific exactly what you want to be deleted.
Also, you most likely won't be able to log in as 'root' if you're used to doing so, as root by default can't connect remotely. (You can enable this, but it's recommended instead to make another admin account and to use that instead).
Note that code may also remotely delete rows, however it depends on the language and implementation as to how it's done.
References:
MySQL DELETE Syntax
MySQL SELECT Syntax (includes details about WHERE)

You can actually use any MySQL client application to connect to remote servers, not only the supplied command-line tool.
If the server allows TCP client connections, there are no differences between running scripts locally or remotely.
If the MySQL server happens to be also a web server, you can use a software like PHPMyAdmin to do it. Once it runs locally (on the same machine as the MySQL server, not your machine), you can work around the problem of not accepting external TCP connections, if the server was configured that way.

Related

How to setup and connect my database to NodeJS application on digital ocean

I have managed to create a droplet on Digital-Ocean and managed to clone my Node JS app onto it. Locally , the app connects to MySQL database and I wanted to the same on the live version. Ignorantly, I attempted to create a Managed database cluster which I did and added 1 user account and created 1 database. Right now I do not know how I can import the exported database.sql file into the database since I am only used to phpMyAdmin.
How can I get this to work and connect to my NodeJS app?
You were using phpmyadmin as an interactive Mysql client program. It's easy to use but hard to set up because it's a web app.
Try another MySql client program. The command-line client, memorably named mysql, is a good choice.
Get a shell on your droplet, then say
sudo apt install -y mysql-client
mysql -u username -p -h databasehostname -D database
mysql> source database.sql
mysql> quit
You'll be prompted for your database password.
That should import your database.
The mysql command line program is very useful and worth some of your time to learn to use.
First, make sure your database cluster is not open to the outside world by adding a DB firewall using DigitalOcean databases. You can allow connections from your own droplet's private IP address, and your own public address (or VPN or however way you're set up). Once you've done that, you should be able to import your SQL file locally (or from the DO Droplet, as long as you have the mysql client installed):
mysql -h [host-provided-do] -P [port-provided-do] -u [username-provided-do] -p [db-name-provided-do] < my-file.sql
The most important thing is to make sure your managed database is not open to the outside world, and that you make sure it only allows incoming connections from known IP addresses.
In your NodeJS app, you can set the driver to connect to the private subnet that DO provides.

MariaDB opens instead of MySQL in cmd prompt

I have been using XAMPP for MySQL which works fine when I am using in browser but if I open MySQL in command-line it opens MariaDB instead of MySQL. Let me know the reason soon.
Looks like Xamp now ships with MariaDB. Check out this article for more information
https://www.apachefriends.org/blog/new_xampp_20151019.html
The MYSQL apache extension works with MariaDB so although it seems apache is using MYSQL i think it is connecting to your MariaDB server.
I wouldn't worry too much. MariaDB runs and works almost identical to MYSQL. What sort of concerns do you have running MariaDB?
If it is an issue have a look at installing and older version of Xamp that contains MYSQL eg
https://sourceforge.net/projects/xampp/
I know it's too late to respond the question , but I also have same problem and you can use either of these options
mysql -u user_name -h host_name mysql -p
replace user_name and host_name with yours
mysql -u user_name -h host_name -p
Then enter
Use mysql;
And that will do it.
That's fine. Try running table fetching commands & it will work fine.
Like "use tablename"
"show table" etc & it will fetch data from MySQL database only

ERROR: must be superuser to alter superusers

Unfortunately, I have removed super user privileges from postgres user in PostgreSQL. And currently I have not any super user in PostgreSQL. And i want to make superuser. So how can i make it ? Every time I am trying to make postgres to super user. I am facing this problem.
Error : must be superuser to alter superusers.
(assuming you have root access on Ubuntu machine)
To enter psql as super user you need to:
sudo -u postgres psql
as suggested in this SO post here
If there is no user called postgres you need to create it on system first, with:
sudo adduser newuser
Else, if you have problems with password not accepted or not created at all you can follow (Ubuntu 14.04 related) instructions here or for more on user accounts look here
For me helps:
sudo -u gleb psql postgres
where gleb is my mac system user
Adding to Craig Ringer's answer, here is the procedure for MacOS and Brew if you accidentally downgrade your only PostgreSQL user:
brew services stop postgresql
Wait a few seconds and/or check Activity Monitor to make sure "postgres" is no longer running.
/usr/local/Cellar/postgresql/10.4/bin/postgres --single -D /usr/local/var/postgres
backend> ALTER USER "yourname" with superuser; or whatever privilege you need to fix
CTRL-D
brew services start postgresql
You're going to have to stop the database system and start a stand-alone back-end, which always unconditionally runs as a superuser.
You can use this backend to ALTER the user you wish to give superuser rights to. Then shut the standalone backend down and start the database normally.
It is important that you completely stop the database server before entering single user mode. PostgreSQL single user mode will refuse to start if there's a postmaster, but to be sure you should make sure there are no PostgreSQL processes running on your system. Under (almost) no circumstances should you ever delete postmaster.pid - that's pretty much guaranteed to result in database corruption if there's still any PostgreSQL process accessing that data directory.
Exactly how to start a standalone back-end depends a bit on your OS/distro and how you installed PostgreSQL. You haven't included this info, so I can only really point you at the manual for the postgres back-end executable.
Make a backup first.
In the single-user mode, the session user will be set to the user with ID 1, and implicit superuser powers are granted to this user. This user does not actually have to exist, so the single-user mode can be used to manually recover from certain kinds of accidental damage to the system catalogs.
See the section Options for Single User mode and, toward the bottom, Usage. You'll want to run the postgres backend with --single, as the unix user that owns the database files, with the path to the datadir. On a typical Linux PostgreSQL install this might be something like:
sudo systemctl stop postgresql-9.3.service
sudo -u postgres /usr/pgsql-9.3/bin/postgres --single -D /var/lib/pgsql/9.3/data
Your datadir and postgres executable location are quite possibly different. The above is for a Fedora system running with PGDG PostgreSQL packages from http://yum.postgresql.org/ .
Assuming that your system user is 'ec2-user'
So try this to enter as superuser
psql -U ec2-user postgres
This will enter you as ec2-user as superuser using postgres db
Now, change postgres user roles to superuser
ALTER USER postgres WITH SUPERUSER;
Quit from above console and now you can open psql using postgres user as superuser
psql -U postgres
Note: I tested this on PostgreSQL 12.5
SELECT usename AS role_name,
CASE
WHEN usesuper AND usecreatedb THEN
CAST('superuser, create database' AS pg_catalog.text)
WHEN usesuper THEN
CAST('superuser' AS pg_catalog.text)
WHEN usecreatedb THEN
CAST('create database' AS pg_catalog.text)
ELSE
CAST('' AS pg_catalog.text)
END role_attributes
FROM pg_catalog.pg_user
ORDER BY role_name desc;
log with root_user then give superuser to postgres

mysql workbench ssh custo command

I'm trying to configure a new connection through SSH tunnel.
But on the server, the command mysql does not exists. I have my own compilation called custo-mysql.
I mean, when I'm on the server, I use the following command :
$ custo-mysql -u root -pmypassword
How can I tell workbench to use custo-mysql and not mysql ?
mysql (and your custo-mysql) is a client for the server. Workbench is a client as well, so it doesn't need to use that custo-mysql thing. It just connects to your server.
Basically, on your server you have a "mysqld" running: a mysql server. Your commans custo-mysql connects to that server as a sort of interface. Workbench has the exact same function, so it should work if your networking/tunneling etc is correct.
As #Nanne already mentioned, you don't need the mysql client to connect to your MySQL server using Workbench.
I would just like to point out that Workbench does use the mysql command line client for importing databases in the Admin / Data Import/Restore section. Having said that, you'll certainly be OK without it for everything else.
Cheers,

Syncing remote database to local?

I'm hoping I can use a shell script that will pull a sql dump down from my production site and into my local database. Ideally, I'd like to be able to run something like this:
sync_site example_com example_local
Where the first argument is the production database and the second is the local database. The remote database is always on the same server, behind SSH, with known MySQL credentials.
Figured it out:
ssh me#myserver.com mysqldump -u user -ppass remote_db | mysql -u user -ppass local_db