THE IT IDEAS

  • Home

Monday, 17 November 2014

MySQL Basic Commands for Linux

 THE IT IDEAS     15:10     basic commands   


How to login into mysql prompt
For Remote MySQL Server.
# [mysql dir]/bin/mysql -h hostname -u root -p
 

e.g. [mysql dir]/bin/mysql -h 192.168.0.100 -u root -p

For Local MySQL Server.
# [mysql dir]/bin/mysql -u root -p

  

How to create a database on the MySql server.

mysql> create database [databasename];

e.g. mysql> create database theitideas;


How to List all databases on the MySQL server.
mysql> show databases;

 


How to Switch to a database to execute query.mysql> use [dbname];

e.g. mysql> use theitidieas;


To see all the tables in the database.
mysql> show tables;


To check database's field formats.
mysql> describe [table name];

e.g.mysql> describe table1;


To delete a database.
mysql> drop database [database name];

e.g. mysql> drop database theitideas;


To delete a table.
mysql> drop table [table name];

e.g. mysql> drop table table1;



Show all data in a table.
mysql> SELECT * FROM [table name];

e.g. mysql> SELECT * FROM table2;



Returns the columns and column information pertaining to the designated table.
mysql> show columns from [table name];
e.g. mysql> show columns from table2;

Show certain selected rows with the value "yes".
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

e.g. mysql> SELECT * FROM table2 WHERE vehicle = "yes";


Show all records containing the name "vansh" AND the phone number '123456'.
mysql> SELECT * FROM [table name] WHERE name = "vansh" AND phone_number = '123456';

e.g. mysql> SELECT * FROM table2 WHERE name = "vansh" AND phone_number = '123456';


Show all records not containing the name "vansh" AND the phone number '123456' order by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "vansh" AND phone_number = '123456' order by phone_number;

e.g. mysql> SELECT * FROM table2 WHERE name != "vansh" AND phone_number = '123456' order by phone_number;


Show all records starting with the letters 'vansh' AND the phone number '123456'.
mysql> SELECT * FROM [table name] WHERE name like "vansh%" AND phone_number = '123456';

e.g. mysql> SELECT * FROM table2 WHERE name like "vansh%" AND phone_number = '123456';


Show all records starting with the letters 'vansh' AND the phone number '123456' limit to records 1 through 5.
mysql> SELECT * FROM [table name] WHERE name like "vansh%" AND phone_number = '123456' limit 1,5;

e.g. mysql> SELECT * FROM table2 WHERE name like "vansh%" AND phone_number = '123456' limit 1,5;



Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

e.g. SELECT * FROM table2 WHERE rec RLIKE "^a";


Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];

e.g. mysql> SELECT DISTINCT address FROM table2;



Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;


e.g. mysql> SELECT 14,15 FROM table2 ORDER BY 15 DESC;



Return number of rows.
mysql> SELECT COUNT(*) FROM [table name];

e.g. mysql> SELECT COUNT(*) FROM table2;



Sum column.

m
ysql> SELECT SUM(*) FROM [table name];

e.g. mysql> SELECT SUM(*) FROM table2;



Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));

e.g. mysql> INSERT INTO user (192.168.1.101,user1,password) VALUES('%','username',PASSWORD('password'));

mysql> flush privileges;



Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

e.g. [mysql dir]/bin/mysqladmin -u root -h 192.168.1.1 -p Password 'Newpassw0rd'


Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');

e.g. mysql> SET PASSWORD FOR 'user'@'192.168.1.100' = PASSWORD('newpassword');

mysql> flush privileges;


Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start



Set a root password if there is NO root password.
# mysqladmin -u root password newpassword



Update a root password.
# mysqladmin -u root -p oldpassword newpassword



Allow the user "vansh" to connect to the server from localhost using the password "Passw0rd". Login as root. Switch to the MySQL db. Give privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to vansh@localhost identified by 'passw0rd';
mysql> flush privileges;



Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;



To update info already in a table.
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';



Delete a row(s) from a table.
mysql> DELETE from [table name] where [field name] = 'whatever';

e.g. mysql> DELETE from table2 where address = 'yes';



Update database permissions/privilages.
mysql> flush privileges;


Delete a column.
mysql> alter table table2 drop column colum1;


Add a new column to db.
mysql> alter table table2 add column column5 varchar (20);


Change column name.
mysql> alter table [table name] change [old column name] [new column name] varchar (50);

e.g. mysql> alter table table2 change column5 column2 varchar (50);



Dump all databases for backup. Backup file is sql commands to recreate all db's.
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

e.g. # [mysql dir]/bin/mysqldump -u root -pPassw0rd --opt >/opt/alldatabases.sql



Dump one database for backup.
# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

e.g. # [mysql dir]/bin/mysqldump -u username -pPassw0rd --databases vansh >/opt/databasename.sql


Dump a table from a database.
# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

e.g. # [mysql dir]/bin/mysqldump -c -u username -pPassw0rd vansh table2 > /opt/vansh.table2.sql



Restore database (or database table) from backup.
# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
e.g. # [mysql dir]/bin/mysql -u username -pPassw0rd vansh1 < /opt/backupfile.sql


Create Table in Database


mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));



OR

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

Like Us !!!

Recent Posts

Blog Archive

  • ►  2016 (1)
    • ►  April (1)
  • ►  2015 (12)
    • ►  August (1)
    • ►  May (2)
    • ►  April (8)
    • ►  March (1)
  • ▼  2014 (23)
    • ►  December (8)
    • ▼  November (12)
      • How to setup Network Configuration in Ubuntu Serve...
      • Amazon RDS Restore MySQL Backup with functions
      • How to install Tomcat in Ubuntu
      • How to install Oracle JAVA JDK with apt-get command
      • How to set environment Variable permenantly in MAC...
      • How to Mount AWS S3 Bucket into AWS EC2 Instance (...
      • How to change DocumentRoot path in Apache Ubuntu 1...
      • How to Benchmarking Webserver
      • How to Choose AWS EC2 Instance
      • SCP Command Example
      • MySQL Basic Commands for Linux
      • How to enable mod rewrite module in apache2
    • ►  April (2)
    • ►  March (1)
  • ►  2013 (7)
    • ►  December (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (3)
  • ►  2012 (15)
    • ►  December (6)
    • ►  November (9)

Categories

apache2 EC2 error mysql php ubuntu aws aws rds browser github installation linux mount s3 AWS Bucket CentOS Computer DocumentRoot change How to Benchmarking Webserver IP LAMP Restart Computer SCP command SQL SERVER XRDP administrator amazon rds backup basic commands chat command line disable download drupal ec2.chroot environment external storage device fstab functions geoip gmail google home page iis instance internet java mod rewrite mongodb multiple login network password one password php5 php5-fpm php7 private browsing python repair filesystem repo reset password rhel sa password same computer script sftp share skype ssh tomcat ubuntu 16.04 ubuntu16 visudo windows 8 youtube
JobsMagBlogJobsMag.InThingsGuide

Popular Posts

  • Install MongoDB php driver in XAMPP/LAMPP
    To install the php driver for MongoDB Prerequisite :: #sudo apt-get install autoconf #export PHP_AUTOCONF=/usr/bin/autoconf #sudo apt...
  • How to Install PHP 7.0, Apache 2.4.18, & MySQL 5.7 on Ubuntu 16.04 LTS
    Update your repo list with below command $ sudo apt-get update -- > Install Apache 2.4.18 $ sudo apt-get install apac...
  • Share Internet from one PC to another PC
    Open Network Connections by clicking the Start button , clicking Control Panel , clicking Network and Internet , clicking Network and S...

Copyright © THE IT IDEAS | Powered by Blogger