- 登陆数据库
[email protected] ~]#mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.38 MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. [[email protected] ~]#mysql -u root -p123456 Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
- 查看数据库版本及当前登陆用户是什么
mysql> select version(); +-----------+ | version() | +-----------+ | 5.5.38 | +-----------+ 1 row in set (0.00 sec) mysql> select user(); +----------------+ | user() | +----------------+ | [email protected] | +----------------+ 1 row in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | activiti_test | | monitor | | mysql | | osa | | osa_guard | | sms | | test | | yandi_monitor | +--------------------+
- 创建GBK字符集的数据库inline,并查看已建库的完整语句。
mysql> create database inline default charset gbk; Query OK, 1 row affected (0.01 sec) mysql> show create database inline; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | inline | CREATE DATABASE `inline` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+----------------------------------------------------------------+ 1 row in set (0.00 sec)
- 创建用户inline,使之可以管理数据库inline;
mysql> grant all on inline.* to [email protected] identified by ‘inline‘; Query OK, 0 rows affected (0.00 sec)
- 查看创建用户inline拥有哪些权限
mysql> show grants for [email protected]; +---------------------------------------------------------------------------------------------------------------+ | Grants for [email protected] | +---------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO ‘inline‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*49129FD3218473FFCCE66D799E25FB6AFD6E0A73‘ | | GRANT ALL PRIVILEGES ON `inline`.* TO ‘inline‘@‘localhost‘ | +---------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
- 进入inline数据库:
mysql> use inline Database changed
- 创建一innodb引擎字符集为GBK表test,字段id和name varchar(16),查看建表结构及SQL语句
mysql> create table test( -> id int, -> name varchar(16)) -> engine innodb default charset gbk; Query OK, 0 rows affected (0.01 sec) mysql> show create table test; +-------+--------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `id` int(11) DEFAULT NULL, `name` varchar(16) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +-------+--------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> show variables like ‘char%‘; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | gbk | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql> desc test -> ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
- 插入一条数据:
mysql> insert into test value(0,‘inlineuser‘); Query OK, 1 row affected (0.00 sec)
- 批量插入数据,要求中文不能为乱码
mysql> insert into test value(1,‘inlineuser‘),(2,‘中文‘); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0
- 查询插入的所有记录
mysql> ?select * from test; +------+------------+ | id | name | +------+------------+ | 0 | inlineuser | | 1 | inlineuser | | 2 | 中文 | +------+------------+ 3 rows in set (0.00 sec)
- 查询id大于1的记录
mysql> select * from test where id>1; +------+--------+ | id | name | +------+--------+ | 2 | 中文 | +------+--------+ 1 row in set (0.00 sec)
- 更新id=1的记录
ysql> update test set name=‘inuser‘ where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from test; +------+--------------+ | id | name | +------+--------------+ | 0 | inlineuser | | 1 | inuser | | 2 | 中文 | +------+--------------+ 3 rows in set (0.00 sec)
- 在字段name前插入age字段,类型int(4)
mysql> alter table test add age int(4) after id; Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | age | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
- 备份inline数据库及mysql数据库
[[email protected] ~]#mysqldump -B inline,mysql >/tmp/backup.sqlmysqldump: Got error: 1049: Unknown database ‘inline,mysql‘ when selecting the database [[email protected] ~]#mysqldump -B inline mysql >/tmp/backup.sql -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. [[email protected] ~]#mysqldump --events -B inline mysql >/tmp/backup.sql
- 删除表中的所有数据。
mysql> delete from test; Query OK, 3 rows affected (0.02 sec)
- 恢复数据
[[email protected] ~]#vim /tmp/backup.sql [[email protected] ~]#mysql </tmp/backup.sql [[email protected] ~]#mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.5.38 MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use inline Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from test; +------+------+--------------+ | id | age | name | +------+------+--------------+ | 0 | NULL | inlineuser | | 1 | NULL | inlineunuser | | 2 | NULL | 中文 | +------+------+--------------+ 3 rows in set (0.00 sec) mysql> desc test -> ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | age | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
- 将inline数据库改为utf8编码
[[email protected] ~]#sed -i "s/gbk/utf8/g" /tmp/backup.sql [[email protected] ~]#vim /tmp/backup.sql [[email protected] ~]#mysql < /tmp/backup.sql [[email protected] ~]#mysql inline Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.5.38 MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> select * from test; +------+------+--------------+ | id | age | name | +------+------+--------------+ | 0 | NULL | inlineuser | | 1 | NULL | inlineunuser | | 2 | NULL | 中文 | +------+------+--------------+ 3 rows in set (0.00 sec) mysql>
- 忘记mysql root用户密码
[[email protected] ~]#/usr/bin/mysqld_safe --skip-grant-tables & mysql> select host,user,password from mysql.user where user=‘root‘; +-----------------------+------+-------------------------------------------+ | host | user | password | +-----------------------+------+-------------------------------------------+ | localhost | root | *509910ED20429303306B7D5FA9215C7EC9E54B68 | | localhost.localdomain | root | | | 127.0.0.1 | root | | +-----------------------+------+-------------------------------------------+ 3 rows in set (0.00 sec) mysql> update mysql.user set password=password(123456) where user=‘root‘ and host=‘localhost‘; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> exit Bye [[email protected] ~]#/etc/init.d/mysqld stop 150920 09:37:22 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended Stopping mysqld: [ OK ] [1]+ Done /usr/bin/mysqld_safe --skip-grant-tables [[email protected] ~]#/etc/init.d/mysqld start Starting mysqld: [ OK ] [[email protected] ~]#mysql -uroot -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.38 MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql>
- 在把id列设置为主键,在Name字段上创建普通索引
mysql> alter table test add primary key idx_p(id); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc test -> ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | age | int(4) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table test add index idx_name(name); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
- 在字段name后插入手机号码字段(moble),类型char(11)
mysql> alter table test add mobile char(11); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc test -> ; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | age | int(4) | YES | | NULL | | | name | varchar(16) | YES | MUL | NULL | | | mobile | char(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> select * from test; +----+------+--------------+--------+ | id | age | name | mobile | +----+------+--------------+--------+ | 0 | NULL | inlineuser | NULL | | 1 | NULL | inlineunuser | NULL | | 2 | NULL | 中文 | NULL | +----+------+--------------+--------+ 3 rows in set (0.00 sec) mysql> insert into test values(3,5,‘foson‘,‘13455556666‘),(4,5,‘foson‘,‘13577889966‘); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+------+--------------+-------------+ | id | age | name | mobile | +----+------+--------------+-------------+ | 0 | NULL | inlineuser | NULL | | 1 | NULL | inlineuser | NULL | | 2 | NULL | 中文 | NULL | | 3 | 5 | foson | 13455556666 | | 4 | 5 | foson | 13577889966 | +----+------+--------------+-------------+ 5 rows in set (0.00 sec)
- 在手机字段上对前8个字符创建普通索引
mysql> alter table test add index idx_m (mobile(8)); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from test; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | test | 0 | PRIMARY | 1 | id | A | 5 | NULL | NULL | | BTREE | | | | test | 1 | idx_name | 1 | name | A | 5 | NULL | NULL | YES | BTREE | | | | test | 1 | idx_m | 1 | mobile | A | 5 | 8 | NULL | YES | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec) mysql> show index from test\G; *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: test Non_unique: 1 Key_name: idx_name Seq_in_index: 1 Column_name: name Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: test Non_unique: 1 Key_name: idx_m Seq_in_index: 1 Column_name: mobile Collation: A Cardinality: 5 Sub_part: 8 Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec) ERROR: No query specified
- 删除name mobile列的索引
mysql> alter table test drop index idx_name; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table test drop index idx_m; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from test\G; *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec) ERROR: No query specified
- 对name列的钱6个字符以及手机列的钱8个字符组建联合索引
mysql> alter table test add index idx_n_m (name(6),mobile(8)); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from test\G; *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: test Non_unique: 1 Key_name: idx_n_m Seq_in_index: 1 Column_name: name Collation: A Cardinality: 5 Sub_part: 6 Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: test Non_unique: 1 Key_name: idx_n_m Seq_in_index: 2 Column_name: mobile Collation: A Cardinality: 5 Sub_part: 8 Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec) ERROR: No query specified
- 有索引及没有索引执行计划的不同
mysql> select * from test where name=‘foson‘ and mobile like ‘135%‘; +----+------+---------+-------------+ | id | age | name | mobile | +----+------+---------+-------------+ | 4 | 5 | foson | 13577889966 | +----+------+---------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘; +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | test | range | idx_n_m | idx_n_m | 46 | NULL | 1 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set (0.01 sec) mysql> alter table test drop index idx_n_m; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 5 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> mysql> alter table test add index idx_n_m (name(6),mobile(8)); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> explain select * from test where mobile like ‘135%‘; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 5 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘; +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | test | range | idx_n_m | idx_n_m | 46 | NULL | 1 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from test where name=‘foson‘; +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ | 1 | SIMPLE | test | ref | idx_n_m | idx_n_m | 21 | const | 2 | Using where | +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ 1 row in set (0.00 sec) mysql>
时间: 2024-10-28 14:28:35