Ubuntu command line (terminal) to operate mysql

Reprinted from: http://blog.csdn.net/lllliulin/article/details/51526569

Mysql installation directory

Database directory
/var/lib/mysql/
configuration file /usr/share/mysql (
mysql.server command and configuration file)
related commands
/usr/bin (mysqladmin mysqldump and other commands)
startup script
/etc/init.d/mysql (startup directory of the script file mysql)

System Management

Connect to MySQL
format: mysql -h host address -u user name -p user password
Example 1: Connect to MySQL on the local machine.
hadoop@ubuntu:~$ mysql -uroot -pmysql;

Example 2: Connect to MYSQL on a remote host.
hadoop@ubuntu:~$ mysql -h 127.0.0.1 -uroot -pmysql;

Change new password

Enter in the terminal: mysql -u username -p password, press Enter to enter Mysql.

use mysql;
update user set password=PASSWORD('new password') where user='username';
flush privileges; #update privilege
quit; #exit

Add new users

Format: grant select on database.* to username@login host identified by 'password'
Example:
Example 1: Add a user test1 with a password of abc, so that he can log in on any host, and have
query , insert, Permission to modify and delete. First connect to MySQL as root user, then type the following commands:
mysql>grant select,insert,update,delete on . to root@localhost identified by 'mysql';
or
grant all privileges on . to root@localhost identified by 'mysql' ';
then refresh the permission settings.
flush privileges;

Example 2: If you do not want root to have a password to operate the data table in the database "mydb", you can type another command to remove the password.
grant select,insert,update,delete on mydb.* to root@localhost identified by ”;

delete users

hadoop@ubuntu:~$ mysql -u username -p password
mysql>delete from user where user='username' and host='localhost';
mysql>flush privileges;
//delete the user's database
mysql>drop database dbname;

database operations

Show all databases
mysql> show databases; (note: there is an s at the end)

Create database
mysql> create database test;

Connect to the database
mysql> use test;

View the currently used database
mysql> select database();

The table information contained in the current database
mysql> show tables; (Note: there is an s at the end)

drop database
mysql> drop database test;

table operations

Note: Use "use <database name>" before the operation to connect to a database.
Create table
command : create table <table name> (<field name 1> <type 1> [,..<field name n> <type n>]);
example:
mysql> create table MyClass(

id int(4) not null primary key auto_increment,
name char(20) not null,
sex int(4) not null default ‘0’,
degree double(16,2));

Get table structure
Command : desc table name, or show columns from table name
Example:
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;

Drop table
command : drop table <table name>
For example: drop the table named MyClass
mysql> drop table MyClass;

Insert data
command : insert into <table name> [( <field name 1>[,..<field name n > ])] values ​​( value 1 )[, ( value n )]
Example:
mysql> insert into MyClass values( 1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);

Query the data in the table
Query all rows
mysql> select * from MyClass;

Query the first few rows of data
For example : view the first 2 rows of data in the table MyClass
mysql> select * from MyClass order by id limit 0,2;
or
mysql> select * from MyClass limit 0,2;

Delete data in the table
Command : delete from table name where expression
For example: delete the record numbered 1 in the table MyClass
mysql> delete from MyClass where id=1;

Modify the data in the table
Command : update table name set field=new value,… where condition
mysql> update MyClass set name='Mary' where id=1;


Add a field to a table Command : alter table table name add field type other;
for example: a field passtest is added to the table MyClass, the type is int(4), the default value is 0
mysql> alter table MyClass add passtest int(4) default '0'

Change the table name
command : rename table original table name to new table name;
for example: change the name of the table MyClass to YouClass
mysql> rename table MyClass to YouClass;

Update field content
command : update table name set field name = new content
update table name set field name = replace(field name, 'old content', 'new content');
for example: add 4 spaces before the
article update article set content= concat(' ', content);

Database import and export

Export database file from database
Use "mysqldump" command to
first enter DOS interface, and then perform the following operations.
1) Export all databases
Format : mysqldump -u [database user name] -p -A>[backup file save path]

2) Export data and data structure
Format : mysqldump -u [database user name] -p [name of database to be backed up]>[path to save the backup file]
Example:
Example 1: Export database mydb to e:\MySQL\mydb .sql file.
Open Start->Run->Enter "cmd" to enter command line mode.
c:> mysqldump -h localhost -u root -p mydb >e:\MySQL\mydb.sql
Then enter the password, wait for a while and the export is successful, you can go to the target file to check whether it is successful.

Example 2: Export mytable in database mydb to e:\MySQL\mytable.sql file.
c:> mysqldump -h localhost -u root -p mydb mytable>e:\MySQL\mytable.sql

Example 3: Export the structure of database mydb to e:\MySQL\mydb_stru.sql file.
c:> mysqldump -h localhost -u root -p mydb –add-drop-table >e:\MySQL\mydb_stru.sql
Note: -h localhost can be omitted, which is generally used on virtual hosts.

3) Only export data without exporting data structure
Format :
mysqldump -u [database user name] -p -t [database name to be backed up]>[backup file save path]

4) Export the Events
format in the database: mysqldump -u [database user name] -p -E [database user name]>[the path to save the backup file]

5) Export the stored procedures and functions in the database
Format : mysqldump -u [database user name] -p -R [database user name]>[the path to save the backup file]

Import into database from external file
1) Use "source" command
First enter "mysql" command console, then create database, then use that database. Finally do the following.
mysql>source [Save path of backup file]

2) Use the "<" symbol
First enter the "mysql" command console, then create a database, then exit MySQL and enter the DOS interface. Finally do the following.
mysql -u root -p < [the path to save the backup file]

Install Navcat

1 Download the file
Official download address: http://www.navicat.com/cn/download/download.html
2 Create a folder /usr/local/navicat, put the decompressed file in the folder,
3 Run ./ start_navicat
4 First of all, there will be garbled characters at the beginning,
open with gedit start_navicat, modify lang = zh_cn.utf-8
5 Crack 
5.1 Download this file http://pan.baidu.com/s/1bVkM5o
5.2 Install wine
I use ubuntu kylin, so right-click to run the file directly in wine, and a selection box will pop up after that, select the location where your navicat is installed, and that's it.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325821917&siteId=291194637