Saturday, May 11, 2013

Operate mysql from command line

Use mysql using command line :

First install mysql client using command line.
apt-get install mysql-client or yum install mysql-client according to your Linux distribution.

Now go on mysql prompt using
$ mysql -h 127.0.0.1

If you want to log-in with root using command line
$ mysql -h 127.0.0.1 -u root

Root's password is blank. If it asks for password, do not write anything, just press Enter key.

$ mysql -h 127.0.0.1 -u root -p


Do not try to log-in with password protected user using command line.

If you try to log-in with password protected user, it gives following error :
ERROR 1045 (28000): Access denied for user 'admin'@'localhost' (using password: YES) 

phpmyadmin error : Script timeout passed

phpMyAdmin: Script timeout passed, if you want to finish import, please resubmit same file and import will resume.





If you are not able to upload large sql files in phpmyadmin because of this error, you need to increase your execution time.

Open config file of your phpmyadmin.

If you are using xampp, you will find the config file inside phpmyadmin directory. complete path : /opt/lampp/phpmyadmin/config.inc.php
If you are using apache2 or httpd
complete path : /usr/share/phpmyadmin/config.inc.php

Add following line in the config file.
$cfg['ExecTimeLimit'] = 0;

Restart Apache & mysql both.

Alternate Solution :
Continue Uploading : If you do not have access to server, you can continue uploading the same gzip, sql, zip, or bzip2 file then the data import will pick up where it left off.

Import mysql database using command line in Linux

Upload mysql database command :

If you want to import a mysql database using command line, here is the command.
$ mysql -h host-name -u username -p'password' "database_name" < "path/of/the/sql/file"

where 
hostname is name of your host. If you upload on your system it can be localhost or 127.0.0.1. If you would upload on server it would be IP of the server.
username is username of mysql user
password is password of mysql user, Remember there is no space between -p & single quote (')
database_name is name of your database in which you want to upload the sql file

Examples:

a) If you want to upload file on your localhost, the command would be (if root has no password)
$ mysql -h 127.0.0.1 -u root "database_name" < /path/of/the/sql/file

b) If you want to upload file on other system of same LAN, the command would be
$ mysql -h 192.168.1.7 -u john -p'johnathan' "webpro" < /home/user/documents/webpro.sql

where 192.168.1.7 - IP of other system of same LAN
john is username of mysql user
johnathan is his password
webpro is database name

NOTE :
Please create database first before running above command otherwise it would give error "ERROR 1049 (42000): Unknown database 'webpro'"