31

I have the following sql command which I usually run in phpmyadmin by selecting the database, then running it in the command window.

DELETE FROM `wp_posts` WHERE `post_type` = "attachment"

However I've never done this via terminal before. My question is, how do I "point" this command to a specific database and then run it?

4
  • 1
    Try mysql -u root -p.
    – Mitch
    Sep 5, 2013 at 6:40
  • 1
    How can you run any command without login to MySQL? First in terminal you have to login using your MySQL username and password Eg: mysql -uroot -p then once loged in select your data base using use <yourdatabasename then you can run this command. Sep 5, 2013 at 6:41
  • You can also put the database in front of the table like so: DELETE FROM {databasename}.wp_posts WHERE post_type` = "attachment"`
    – Rinzwind
    Sep 5, 2013 at 7:24
  • See also related question: askubuntu.com/q/1077725/295286 Nov 13, 2018 at 17:36

7 Answers 7

41

For you to run it from terminal, you need to be logged into mysql first and then you execute your program. The ideal code would be

mysql -u(username) -p(password) (the name of the database) -e "DELETE FROM `wp_posts` WHERE `post_type` = "attachment""

I`m sure this works, hope it helps

2
  • 2
    You need to escape the backticks using \ if executing from a normal terminal
    – myol
    Mar 14, 2016 at 17:31
  • How about e.g. dropping a database and creating it again?
    – Jeppe
    Apr 13, 2020 at 8:17
18

I think you have forgot your user name for mySQL. so please enter the correct user name and password when you type mysql command. for eg

$ mysql   -u  root   -p 
Enter Password:

Note: Default user name and password is root

2
  • 1
    Default root's access need sudo, sudo mysql -u root -p Oct 31, 2017 at 12:26
  • Default root's access need sudo, sudo mysql -u root -p Oct 31, 2017 at 12:27
10

Just type the following command in terminal to use mysql interpreter:

mysql -u root -p database_name

Enter your password, then you can run your mysql commands.

Another way is to use:

mysql -u root -p database_name << eof
DELETE FROM `wp_posts` WHERE `post_type` = "attachment"
eof
6

Try this:

mysql --host *HOST_IP* --user *USER* --password=*PASSWORD* -D *DATABASE_NAME* -e "DELETE FROM `wp_posts` WHERE `post_type` = "attachment" ;
2

Using the MySQL CLI client.

$ mysql -u <user> -p
mysql> use <your-database>
mysql> DELETE FROM `wp_posts` WHERE `post_type` = "attachment"

Notice that I'm using -u and username, but in -p I'm not setting anything. MySQL will ask you for your password.

And if you're trying to use it inline, you can use

$ mysql -u <user> -p <your-database> -e "DELETE FROM `wp_posts` WHERE `post_type` = 'attachment'"

Here, notice that I'm using also -e argument. It stands for execute.

Pro tip

In MySQL client you can use:

mysql> show databases;

if you want to view all dabatases in your server. And you can use:

mysql> use <database>;
mysql> show tables;

if you want to view all tables into a database.

I'm using $ and mysql> as prompts, $ means your Linux terminal, and mysql>, MySQL client.

-1

There is a series of steps to Execute MySQL in Linux Ubuntu Terminal.

  1. Execute MySQL Client using the following command: mysql -u root -p
  2. It is important to Create a New Database first using the command: create database demo_db;
  3. Then you have to Authorize the Database using the command: grant all on demo_db.* to ‘testuser’ identified by ‘12345’;
  4. Then Log in from the Database itself using the command: mysql -u testuser -p
  5. Then, you can actually start with the actual SQL Commands.

I referred to the article http://www.codingalpha.com/run-mysql-database-linux-ubuntu/ where they have explained perfect steps to Execute MySQL in Linux Ubuntu! Hope I got your MySQL Running Perfectly.

1
  • 1
    Please note that mysql is the client, and not the server. Before starting the client, check that the server is running by issuing sudo status mysql and if it is not running, start it with sudo start mysql.
    – Guss
    Dec 31, 2015 at 20:37
-2

Okay basically it was "SELECT databasename;" then executing my command.

1
  • 1
    You can also specify the database name on the command line, such as mysql -u root -p databasename - enter password when prompted and you are using the database. Sep 5, 2013 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.