Foreword
I believe there we will use the MySQL database at the time of development, it is more fire a database tool for most businesses, it's business, MySQL can be perfectly supported.
Many times we are operating aid visualization tools mysql mysql, although that is more convenient, but remember that some commonly used command is still necessary.
We summarize some of the more commonly used statements usually command:
1, change the root password
mysqladmin -uroot password 'new_password'
2, log on MySQL server
mysql -h120.0.0.1 -uroot -p*** -p3306
3, list all databases
show databases;
4, enter a database
use database_name ; (数据库名)
5, lists the tables in a database
show tables;
6, view a list of all of the fields
show create table table_name; (还可以展示建表语句)
desc table_name;
7, view the current user
select user();
8, view the current location data
select database();
9, create a new database and specify the character set
create database db_name charset utf8;
10, create a new table
CREATE TABLE `table_name`
(`use_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`card_id` int(11) unsigned NOT NULL,
`card_user_id` int(11) unsigned NOT NULL,
`amount` int(4) NOT NULL,`note` text,
PRIMARY KEY (`use_id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
11. Notes the statement
--说明:注释是使用两个- 并且要用空格隔开;
12, view the database version
select
13, check the database status
show status; -- 当前会话状态
show global status; -- 全局数据库状态
show slave statusl\G; -- 查看主从数据库状态信息
说明:结尾使用\G 可以格式化查询结果的输出
14, query the database parameters
show variables;
15, modify the database parameters
show variables like 'max_connect%';
set global max_connect_errors = 500; (重启数据库会失效,需要再配置文件中修改)
16, view the current queue database statement
show processlist;
17. Create a regular user and licensed to a database
grant all on db_name.* to 'username' @ 'ip_Host' identified by 'password';
18, query data table data
select * from db_name limit 1; -- 查询表中1行数据的所有字段
select count(*) from db_name; -- 查询表中的行数
select * from db_name where a like '123%' or a in (1,3,4) and a != 222; -- 查询语句中使用条件查询
select a.* ,b.* from db1 as a left join db2 as b on a.id = b.id where a.id = 123; -- 左查询,以a表为准,与b表匹配查询
19, insert a row
insert into db_name values(1,'user');
20, the update statement
update db_name set name = 'dbuser' where id = 1;-- 切记更新语句要有where条件来限制范围
21, empty table data
truncate table db_name;-- 慎用
22, delete the table
drop table db_name;
23, all tables in the database
mysql -N -s infomation_schema -e "SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME,';')" FROM TABLES WHERE TABLE_SCHEMA = 'database_name' | mysql -f database_name
24, delete the database
drop database db_name;
25, Database Backup
mysqldump -uroot -p'password' mysql > /tmp/mysql_backup.sql -- 导出为sql语句
26, database recovery
mysql -uroot -p'password' mysql < /tmp/mysql_backup.sql -- 导入sql语句
27, change the user name user password
SET PASSWORD FOR user = PASSWORD('password');
28, see the user's user rights
SHOW GRANTS FRO user;
29 sql statement, the import sql file
source /tmp/back.sql
30, mysql command script execution
mysql -uroot -ppassword -e "show databases"
echo "show databases" | mysql -uroot -ppassword
执行大量mysql语句使用的方式
mysql -uroot -proot << EOF
mysql语句1;
mysql 语句2;
...
EOF
At last
It summarizes the above statements and commonly used commands, mysql but there are many things not related to,
Such as transactions, stored procedures, implementation of the master-slave class, I usually want to look at the books mysql, and its implementation under the principle of understanding.
Here I recommend two books to read (can be public number Message receive electronic PDF version):
High Performance MySQL (3rd Edition)
MySQL_ build robust high-availability data center
Related Reading
PHP + MySQL to achieve some conclusions importing and exporting of mass data
Multi-process PHP scripts to achieve massive data transfer summary
---------------------------------------------------END----------------------------------------------------
I welcome the attention of the public number
Constantly updated variety of technical experience, to provide a variety of learning resources for free!