标签:lnmp架构 lamp架构 mysql编译安装 mysql优化
insert插入语句 mysql> create table test (id int(4) not null auto_increment,name char(20) not null,primary key (id)); mysql> insert into test(id,name) values(1,‘martin‘); Query OK, 1 row affected (0.03 sec) mysql> insert into test(name) values(‘martin‘); #由于id字段是自增的,所以可以只插入name字段 Query OK, 1 row affected (0.02 sec) mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | martin | | 2 | martin | +----+--------+ 2 rows in set (0.02 sec) mysql> insert into test values(3,‘marry‘); Query OK, 1 row affected (0.02 sec) mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | martin | | 2 | martin | | 3 | marry | +----+--------+ 3 rows in set (0.00 sec) mysql> insert into test values(4,‘marry‘),(5,‘jacky‘); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | martin | | 2 | martin | | 3 | marry | | 4 | marry | | 5 | jacky | +----+--------+ 5 rows in set (0.00 sec) select查询语句 mysql> select host,password,user from mysql.user; +-------------+-------------------------------------------+--------+ | host | password | user | +-------------+-------------------------------------------+--------+ | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | | ansible | | root | | 127.0.0.1 | | root | | ::1 | | root | | localhost | | | | ansible | | | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | martin | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | +-------------+-------------------------------------------+--------+ 8 rows in set (0.01 sec) mysql> select host,password,user from mysql.user where host=‘ansible‘; +---------+----------+------+ | host | password | user | +---------+----------+------+ | ansible | | | | ansible | | root | +---------+----------+------+ 2 rows in set (0.04 sec) mysql> select host,password,user from mysql.user where host=‘ansible‘ limit 1; +---------+----------+------+ | host | password | user | +---------+----------+------+ | ansible | | | +---------+----------+------+ 1 row in set (0.00 sec) mysql> select host,password,user from mysql.user order by user; +-------------+-------------------------------------------+--------+ | host | password | user | +-------------+-------------------------------------------+--------+ | localhost | | | | ansible | | | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | martin | | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | | ansible | | root | | 127.0.0.1 | | root | | ::1 | | root | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | +-------------+-------------------------------------------+--------+ 8 rows in set (0.01 sec) mysql> select host,password,user from mysql.user order by user desc; +-------------+-------------------------------------------+--------+ | host | password | user | +-------------+-------------------------------------------+--------+ | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | | ansible | | root | | 127.0.0.1 | | root | | ::1 | | root | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | root | | 172.16.80.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | martin | | localhost | | | | ansible | | | +-------------+-------------------------------------------+--------+ 8 rows in set (0.02 sec) 利用explain查看sql语句的执行计划 mysql> desc test; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | +-------+----------+------+-----+---------+----------------+ 2 rows in set (0.06 sec) mysql> explain select * from test where name = ‘marry‘\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 5 Extra: Using where 1 row in set (0.00 sec) mysql> explain select * from test where id = 3\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: const possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: 1 row in set (0.02 sec) update语句 mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | martin | | 2 | martin | | 3 | marry | | 4 | marry | | 5 | jacky | +----+--------+ 5 rows in set (0.00 sec) mysql> update test set name=‘tom‘ where id=3; #注意where条件,如果不加where的话,会更改所有列 Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | martin | | 2 | martin | | 3 | tom | | 4 | marry | | 5 | jacky | +----+--------+ 5 rows in set (0.00 sec) [root@localhost mysql]# mysqlbinlog mysql-bin.000001 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #160811 23:07:23 server id 1 end_log_pos 106 Start: binlog v 4, server v 5.1.73-log created 160811 23:07:23 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ‘ q5SsVw8BAAAAZgAAAGoAAAABAAQANS4xLjczLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAACrlKxXEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC ‘/*!*/; # at 106 #160811 23:09:57 server id 1 end_log_pos 193 Query thread_id=2 exec_time=1 error_code=0 SET TIMESTAMP=1470928197/*!*/; SET @@session.pseudo_thread_id=2/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C latin1 *//*!*/; SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create database martin /*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; [root@localhost mysql]# mysqladmin -uroot -p‘‘ flush-log #执行这条语句时会将bin-log日志切割,如下面就生成了新得的日志mysql-bin.000002 binlog是不会记录查询操作的,只记录更改的 Enter password: [root@localhost mysql]# ll total 20504 -rw-rw---- 1 mysql mysql 10485760 Aug 11 23:07 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Aug 11 23:07 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Jul 3 23:22 ib_logfile1 drwx------ 2 mysql mysql 4096 Aug 11 23:09 martin drwx------ 2 mysql mysql 4096 Jul 3 23:22 mysql -rw-rw---- 1 mysql mysql 236 Aug 11 23:14 mysql-bin.000001 -rw-rw---- 1 mysql mysql 106 Aug 11 23:14 mysql-bin.000002 -rw-rw---- 1 mysql mysql 38 Aug 11 23:14 mysql-bin.index srwxrwxrwx 1 mysql mysql 0 Aug 11 23:07 mysql.sock drwx------ 2 mysql mysql 4096 Jul 3 23:28 test [root@localhost mysql]# mysqlbinlog -d martin mysql-bin.000001 > bin.sql #-d 指定数据库 删除bin.sql中需要删除的语句 [root@localhost mysql]# mysql -uroot -p‘‘ test < bin.sql truncate table 表名; 清空表中所有内容,更快,清空物理文件 delete from 表名; 逻辑清除,按行删 增删改表的字段 alter table 表名 add 字段 类型 其他; mysql> desc Staff; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> alter table Staff add sex char(4); Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> desc Staff; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | sex | char(4) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) 更改表名 rename table 原表名 to 新表名; mysql> rename table Staff to martin; Query OK, 0 rows affected (0.00 sec)
本文出自 “厚德载物” 博客,谢绝转载!
标签:lnmp架构 lamp架构 mysql编译安装 mysql优化
原文地址:http://huaxin.blog.51cto.com/903026/1837490