Alex Rivera | Logout

How to log in to MySQL and query the database from Linux terminal?

Asked 2011-06-01T11:11:28.310
124

I am using Debian Linux on which MySQL is installed. I can log in to my Linux machine using root user as well as other user. I can connect to MySQL database on Linux machine from Windows machine using SQLyog. Now I want to execute queries on Linux machine only using Linux terminal command line.

I tried some following things on terminal

I went to root directory then I went to /var/lib directory

I run following commands on terminal

mysqladmin -u root -p
mysqladmin -u root -ppassword

everytime I have tried, I get the following error message:

ERROR 1045 (28000) Access denied for user 'root'@'localhost' (Using password NO)

How can I accomplish the following?:

  1. How do I get MySQL prompt in Linux terminal?
  2. How I stop the MySQL server from Linux terminal?
  3. How I start the MySQL server from Linux terminal?
  4. How do I get MySQL prompt in Linux terminal?
  5. How do I login to MySQL server from Linux terminal?
  6. How do I solve following error?

ERROR 1045 (28000) Access denied for user 'root'@'localhost' (Using password NO)

Edit
Report

3 Answers

208

1.- How do I get mysql prompt in linux terminal?

mysql -u root -p

At the Enter password: prompt, well, enter root's password :)

You can find further reference by typing mysql --help or at the online manual.

2. How I stop the mysql server from linux terminal?

It depends. Red Hat based distros have the service command:

service mysqld stop

Other distros require to call the init script directly:

/etc/init.d/mysqld stop

3. How I start the mysql server from linux terminal?

Same as #2, but with start.

4. How do I get mysql prompt in linux terminal?

Same as #1.

5. How do I login to mysql server from linux terminal?

Same as #1.

6. How do I solve following error?

Same as #1.

answered 2011-06-01T11:15:07.443
15

To your first question:

mysql -u root -p

or

mysqladmin -u root -p "your_command"

depending on what you want to do. The password will be asked of you once you hit enter! I'm guessing you really want to use mysql and not mysqladmin.

For restarting or stopping the MySQL-server on linux, it depends on your installation, but in the common debian derivatives this will work for starting, stopping and restarting the service:

sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql restart
sudo /etc/init.d/mysql status

In some newer distros this might work as well if MySQL is set up as a deamon/service.

sudo service mysql start
sudo service mysql stop
sudo service mysql restart
sudo service mysql status

But the question is really impossible to answer without knowing your particular setup.

answered 2011-06-01T11:22:38.810
1

To stop or start mysql on most linux systems the following should work:

/etc/init.d/mysqld stop

/etc/init.d/mysqld start

The other answers look good for accessing the mysql client from the command line.

Good luck!

answered 2011-06-01T11:24:16.227

Your Answer