linux view database and table mysql command

Check if a database is installed : rpm -qa|grep -i mysql

Installed as shown:

Not Installed:

Four ways to check MySQL version

1 Execute mysql -V under the terminal

2 Find mysql --help |grep Distrib in help

3 View select version() in mysql;

4 Check the status in mysql;

mysql -h localhost -u username -p password //Link database
use desk_show; //Use database
show tables; //Display data table
describe desk6_0; //Display table structure

View by rpm

Check if the software is installed

First, we need to check whether the software has been installed, or the name of the installed package. For example, to find out whether to install mysql

 

Then list the files installed by the package according to rpm -ql

Use rpm -qal |grep mysql to view the file storage locations of all mysql installation packages

Yum find

In addition to rpm query, you can also use yum search to find the corresponding packages that can be installed

Other ways to find

In addition to finding the file location according to the software package, the most commonly used method is to find a keyword such as mysql all file paths containing the mysql service.

Which lookup command

Which command is to find the executable file path through the PATH environment variable, which is used to find the folder where the command is located

The whereis command is similar to find , but the difference is that whereis is faster to search through a locally structured database index. If there is no file or command updated to the database, the information cannot be found

 

 

mysql other commands:

 

show databases; show databases

 

create database name; create database

 

use databasename; select database

 Execute the command source /root/20151010.sql

drop database name delete the database directly without reminding

  

show tables; show tables

  

describe tablename; show the specific table structure

Add distinct to select to remove duplicate fields

 

mysqladmin drop databasename 

Before deleting the database, there is a prompt.

 

Display current mysql version and current date

select version(),current_date; 

 

Root password management     
Set the root user's password mysqladmin -uroot password 'password'
to modify the root user's password mysqladmin -uroot -p password 'password'
 
database, table management     
into mysql mysql -h hostname -uroot -p
to create a database mysql> create database location
Import data structure mysql -uroot -p location <./location.sql
view database mysql> show databases;
enter a database mysql> use location;
view table information mysql> show tables;
view table structure mysql> desc Contact;
change table name mysql> rename table Contact to Contact_new
delete database mysql> drop database location
delete table mysql> drop table Contact
 
authorization part     
establish user and authorize mysql> grant all on location.* to gk1020@'10.1.11.71' identified by 'gk1020'
Cancel authorization mysql> revoke all on location.* from gk1020@'10.1.11.71'
mysql> flush privileges
 
operation statement     
query mysql> select * from Contact
mysql> select count(*) from Contact
modify mysql> update Contact set RegTime= '2008-01-01 00:00:00' where id=1
mysql> update Contact set RegTime='2008-01-01 00:00:00', CID=1 where id=1
insert mysql> insert into Contact values ('',''...)
mysql> insert into Contact(ID, CID, Contact) values('',''...)
delete mysql> delete from Contact where id=1
 
backup database location mysqldump -uroot -p -l location >./location.sql
backup a table mysqldump -uroot -p -l --database location --table Contact >./contact.sql
Export the data structure of the database location mysqldump -d -uroot -p location >./location.sql
to restore the database: mysql -uroot -p location < location.sql
to separate the backup data file from the sql file: mysqldump -uroot -p location contact -T backup/
export data: select * from contact into outfile 'contact.txt';
restore sql file: cat contact.sql | mysql -uroot -p location
to import data (to follow the absolute path) mysqlimport -uroot -p zhang /usr/local /mysql/var/backup/contact.txt
 
copy table Copy table Contact as Contact_bak mysql> create table Contact_bak as select * from Contact
Copy the structure of table Contact mysql> create table Contact_bak as select * from Contact where 1=2

view the execution task mysql> show processlist
kill executing task mysqladmin -uroot -p kill process_id

Check table status: mysql> check table Contact or mysqlcheck -uroot -p -c location Contact 
Repair table: mysql> repair table Contact or mysqlcheck -uroot -p -r location Contact 
Optimize table: mysql> optimize table Contact or mysqlcheck -uroot - p -o location Contact 

 

MySQL mysqldump import/export structure & data & stored procedures & functions & events & triggers

———————————————-Library operation ———————————————-
1.①Export a library structure

mysqldump -d dbname -u root -p > xxx.sql

②Export multiple library structures

mysqldump -d -B dbname1 dbname2 -u root -p > xxx.sql

 

2. ① Export a library data

mysqldump -t dbname -u root -p > xxx.sql

②Export multiple database data

mysqldump -t -B dbname1 dbname2 -u root -p > xxx.sql

 

3. ① Export a library structure and data

mysqldump dbname1 -u root -p > xxx.sql

②Export multiple library structures and data

mysqldump -B dbname1 dbname2 -u root -p > xxx.sql



———————————————--Table Operation ———————————————-
4. ① Export a table structure

mysqldump -d dbname1 tablename1 -u root -p > xxx.sql

②Export multiple table structures

mysqldump -d -B dbname1 --tables tablename1 tablename2 -u root -p > xxx.sql

 

5. ① Export a table data

mysqldump -t dbname1 tablename1 -u root -p > xxx.sql

②Export data from multiple tables

mysqldump -d -B dbname1 --tables tablename1 tablename2 -u root -p > xxx.sql

 

6. ① Export a table structure and data

mysqldump dbname1 tablename1 -u root -p > xxx.sql

②Export multiple table structures and data

mysqldump -B dbname1 --tables tablename1 tablename2 -u root -p > xxx.sql



——————————————Stored procedure & function operation ————————————-
7. Only export stored procedures and functions (do not export structure and data, and export structure at the same time If so, you need to use -d)

mysqldump -R -ndt dbname1 -u root -p > xxx.sql



———————————————-Event Operation ———————————————-
8. Only export events

mysqldump -E -ndt dbname1 -u root -p > xxx.sql



————————————– Trigger Actions ———————————————–
9. Do not export triggers (triggers are exported by default – triggers, use – skip-triggers mask export triggers)

mysqldump --skip-triggers dbname1 -u root -p > xxx.sql



————————————————————————————————————
10. Import

mysql -u root -p
use game;
source xxx.sql



———————————————————————————————————
To summarize:

-d structure (--no-data: do not export any data, only export database table structure)

-t data (--no-create-info: only export data without adding CREATE TABLE statement)

-n (--no -create-db: only export data without adding CREATE DATABASE statement)

-R (--routines: export stored procedures and custom functions)

-E (--events: export events)

--triggers (default export triggers, Use --skip-triggers to shield the export)

-B (--databases: export database list, can be omitted for a single database)

--tables table list (can be omitted for a single table)

①When exporting structure and data at the same time, you can omit -d and -t at the
same time ②Do not export structure and data at the same time, you can use -ntd ③Export
only stored procedures and functions, you can use -R -ntd ④Export
all (structure&data&stored procedure& Functions & events & triggers) use -R -E (equivalent to ①, omit -d -t; triggers are exported by default)
⑤ Only export structures & functions & events & triggers use -R -E -d

PS: If you can use related tools, such as the official MySQL Workbench, it is very convenient to import and export, as shown below. (Of course, for security, external operation permissions are generally shielded, so there are more cases where commands need to be used)

 

Import encountered a problem ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes

When using Navicat For Mysql backup to create a new database, the table cannot be imported, check the log, there is a script error, it turns out that the file is larger than 16M

The error message is as follows: ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes.

 

So adjust the configuration file: /etc/my.cnf

To modify the maximum allowable packet size of mysql, 
add a sentence in the [mysqld] section (it is useless if it is not used in this section): 
max_allowed_packet=50M 
to restart the mysql service.

The restart command is as follows:

1. Startup method 
1. Start with service: service mysqld start 
2. Start with mysqld script: /etc/inint.d/mysqld start 
3. Start with safe_mysqld: safe_mysqld& 
2. Stop 
1. Start with service: service mysqld stop 
2. Start with mysqld script: /etc/inint.d/mysqld stop 
3. mysqladmin shutdown 
3. Restart 
1. Start with service: service mysqld restart 
2. Start with mysqld script: /etc/inint.d/mysqld restart

 

To migrate a server, copy the mysql data on the old server to the mysql on the new server.

I first copied the database of the old server with mysqldump, and then I wanted to import it to the new server, import it with the command, and the result prompted

 

The program said on the Internet, I added to the command line--max_allowed_packet=128M

mysql --max_allowed_packet=128M -u user -ppass database < database.sql

The result still prompts Got a packet bigger than 'max_allowed_packet' bytes

Then I tried to import with MysqlWorkBench client, but it still prompted Got a packet bigger than 'max_allowed_packet' bytes .

It's so strange, obviously this .sql file is only 82K!

solution

mysql -u root -p -e "set global net_buffer_length=1000000; set global max_allowed_packet=1000000000;"

Then restart mysqld, and then import normally...

Guess you like

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