一、設(shè)置更改root密碼
第一次進(jìn)入數(shù)據(jù)庫不需要密碼:
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot //-u 指定要登錄的用戶,后面有無空格都行;root為mysql自帶的管理員賬號(hào),默認(rèn)沒有密碼
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> quit //退出時(shí)直接輸入quit或exit即可
Bye
上面命令使用了絕對(duì)路徑,不是很方便,為了更方便,可以修改/etc/profile把/usr/local/mysql/bin加入到環(huán)境變量中:
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
這樣就可以不用每次使用絕對(duì)路徑了。
給root用戶設(shè)定密碼:
[root@localhost ~]# mysqladmin -uroot password '123456' //設(shè)定密碼為123456
Warning: Using a password on the command line interface can be insecure. //這行為警告信息,意思是在命令行下面暴露了密碼,不安全
[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) //再次登錄,提示錯(cuò)誤,因?yàn)闆]有密碼
重新輸入密碼登錄:
[root@localhost ~]# mysql -uroot -p //-p 后面不可以有空格,可以直接跟密碼,也可以不跟,不跟密碼就是以交互形式輸入密碼
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
更改root用戶密碼:
mysql> SET PASSWORD FOR 'root'@localhost =PASSWORD('1234567'); //之前密碼為123456,現(xiàn)在改為1234567
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) //輸入123456,提示錯(cuò)誤
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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. //輸入1234567,成功登陸
忘記root用戶密碼時(shí)修改密碼:
[root@localhost ~]# vim /etc/my.cnf
skip-grant //在[mysqld]下面增加這一行
[root@localhost ~]# /etc/init.d/mysqld restart //重啟服務(wù)
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql -uroot //進(jìn)入mysql
mysql> use mysql; //進(jìn)入mysql庫
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> update user set password = password ('1234567') where user='root'; //修改root密碼
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
[root@localhost ~]# vim /etc/my.cnf //去掉skip-grant這行
[root@localhost ~]# /etc/init.d/mysqld restart //重啟服務(wù)
[root@localhost ~]# mysql -uroot -p //使用新密碼登陸
二、連接MySQL
上面我們使用mysql -uroot -p命令來連接數(shù)據(jù)庫,但是連接的只是本地?cái)?shù)據(jù)庫的localhost。而很多時(shí)候,我們需要連接網(wǎng)絡(luò)中某一主機(jī)上的mysql。
[root@localhost ~]# mysql -uroot -p -h192.168.33.128 -P3306 //-h 指定遠(yuǎn)程主機(jī)的IP,-P(大寫)用來指定遠(yuǎn)程主機(jī)mysql的綁定端口
Enter password:
三、MySQL常用命令
在日常工作中,難免會(huì)遇到一些與mysql相關(guān)的操作,如建庫、建表、查詢mysql狀態(tài)等,我們需要掌握關(guān)于這些常用的命令。
查詢當(dāng)前庫
mysql> show databases; //注意每條命令的最后面都需要跟一個(gè)分號(hào)作為結(jié)束符號(hào)
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.01 sec)
查詢某個(gè)庫的表
先要切換到某個(gè)庫里:
mysql> use mysql; //切換庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A //提示會(huì)把當(dāng)前庫里的所有表的字段全部讀一段,可以在啟動(dòng)mysql時(shí)加上-A關(guān)閉這個(gè)提示,不關(guān)閉也無影響
Database changed
然后再把表列出來:
mysql> show tables;
+—————————+
| Tables_in_mysql |
+—————————+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+—————————+
28 rows in set (0.00 sec)
查看某個(gè)表的全部字段
mysql> desc db; //查看db表的全部字段
+———————–+—————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+———————–+—————+——+—–+———+——-+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+———————–+—————+——+—–+———+——-+
22 rows in set (0.00 sec)
另外,還可以使用這條命令:
mysql> show create table dbG //這個(gè)命令顯示信息更詳細(xì),且會(huì)把建表語句全部列出來; G讓列出來的結(jié)果豎排顯示,這樣看起來更清晰,用了G就不用加分號(hào)了。
*************************** 1. row ***************************
Table: db
Create Table: CREATE TABLE `db` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`Db`,`User`),
KEY `User` (`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Database privileges'
1 row in set (0.00 sec)
查看當(dāng)前是哪個(gè)用戶
mysql> select user(); //查看當(dāng)前用戶
+—————-+
| user() |
+—————-+
| root@localhost |
+—————-+
1 row in set (0.00 sec)
查看當(dāng)前所使用的數(shù)據(jù)庫
mysql> select database(); //查看當(dāng)前數(shù)據(jù)庫
+————+
| database() |
+————+
| mysql |
+————+
1 row in set (0.00 sec)
創(chuàng)建一個(gè)新庫
mysql> create database db1; //新建一個(gè)庫db1
Query OK, 1 row affected (0.00 sec)
創(chuàng)建一個(gè)新表
mysql> use db1 //切換到庫db1
Database changed
mysql> create table t1 (`id` int(4),`name` char(40)); //新建表t1,并且寫入數(shù)據(jù),字段名id和name用反引號(hào)括起來
Query OK, 0 rows affected (0.01 sec)
查看當(dāng)前數(shù)據(jù)庫的版本
mysql> select version(); //查看當(dāng)前mysql版本
+———–+
| version() |
+———–+
| 5.6.36 |
+———–+
1 row in set (0.00 sec)
查看MySql的當(dāng)前狀態(tài)
mysql> show status; //查看當(dāng)前mysql狀態(tài)
+———————————————–+————-+
| Variable_name | Value |
+———————————————–+————-+
| Aborted_clients | 0 |
| Aborted_connects | 2 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 0 |
| Bytes_received | 1124 |
| Bytes_sent | 25602 |
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
…
查看MySql的參數(shù)
mysql> show variables; //查看mysql各參數(shù)
| innodb_stats_sample_pages | 8 |
| innodb_stats_transient_sample_pages | 8 |
| innodb_status_output | OFF |
| innodb_status_output_locks | OFF |
| innodb_strict_mode | OFF |
| innodb_support_xa | ON |
| innodb_sync_array_size | 1 |
| innodb_sync_spin_loops | 30 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 0 |
| innodb_thread_sleep_delay | 10000 |
| innodb_tmpdir |
…
修改MySql的參數(shù):
上面列出的很多參數(shù)都是可以在/etc/my.cnf中定義的。
以參數(shù) max_connect_errors為例,修改它:
mysql> show variables like 'max_connect%'; //mysql中,符號(hào)%類似于shell下的*,表示通配
+——————–+——-+
| Variable_name | Value |
+——————–+——-+
| max_connect_errors | 100 |
| max_connections | 151 |
+——————–+——-+
2 rows in set (0.00 sec)
mysql> set global max_connect_errors = 1000; //修改max_connect_errors的值為1000,set global可以臨時(shí)修改一些參數(shù),重啟mysql服務(wù)失效,修改配置文件my.cnf才能永久生效
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'max_connect%';
+——————–+——-+
| Variable_name | Value |
+——————–+——-+
| max_connect_errors | 1000 |
| max_connections | 151 |
+——————–+——-+ //max_connect_errors的值為1000
2 rows in set (0.00 sec)
查看當(dāng)前MySql服務(wù)器的隊(duì)列
查看服務(wù)器隊(duì)列在日常工作中用的最多,使用它可以查看mysql當(dāng)前在干什么,也可以發(fā)現(xiàn)是否有鎖表。
查看服務(wù)器隊(duì)列:
mysql> show processlist; //查看服務(wù)器隊(duì)列
+—-+——+———–+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——+———–+——+———+——+——-+——————+
| 11 | root | localhost | db1 | Sleep | 9373 | | NULL |
| 12 | root | localhost | NULL | Query | 0 | init | show processlist |
+—-+——+———–+——+———+——+——-+——————+
2 rows in set (0.00 sec)
四、MySQL用戶管理
創(chuàng)建一個(gè)普通用戶并授權(quán)
mysql> grant all on *.* to user1 identified by '123456';
Query OK, 0 rows affected (0.00 sec)
1. all表示所有的權(quán)限(如讀、寫、查詢、刪除等操作);
2. . 旁邊有兩個(gè)匹配條件,前面的*表示所有的數(shù)據(jù)庫,后面的*表示所有的表;
3. identified by后面跟密碼,用單引號(hào)括起來,這里user1指的是localhost上的user1
給網(wǎng)絡(luò)上其他機(jī)器上的某用戶授權(quán):
mysql> grant all on db1.* to 'user2'@'192.168.33.128' identified by '111222';
Query OK, 0 rows affected (0.00 sec)
用戶和主機(jī)的IP都用單引號(hào)括起來,兩者之間有@符號(hào),IP可以用%代替,表示所有主機(jī)
五、常用SQL語句
查詢語句select
第一種形式:
mysql> select count(*) from mysql.user;
+———-+
| count(*) |
+———-+
| 8 |
+———-+
1 row in set (0.00 sec)
//mysql.user 表示mysql庫的user表,count(*)表示表中共有多少行
第二種形式:
mysql> select * from mysql.db;
+—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+
| Host | Db | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+
| % | test | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |
| % | test_% | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |
| 192.168.33.128 | db1 | user2 | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
+—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+
3 rows in set (0.00 sec)
//上面表示查詢mysql庫的db表中的所有數(shù)據(jù),謹(jǐn)慎使用該命令,因?yàn)樾枰馁M(fèi)很大的cpu資源
查詢單個(gè)字段或多個(gè)字段:
mysql> select db from mysql.db //查詢單個(gè)字段
mysql> select db,user from mysql.db //查詢多個(gè)字段
使用%查詢:
mysql> select * from mysql.db where host like '192.168.%' //查詢IP為192.168網(wǎng)段的mysql庫的db表中的所有數(shù)據(jù)
插入一行insert
插入操作在mysql中也很常見。
– 插入:
mysql> insert into db1.t1 values (1,'abc'); //向db1庫的t1表中插入1,abc
Query OK, 1 row affected (0.01 sec)
mysql> select * from db1.t1;
+——+——+
| id | name |
+——+——+
| 1 | abc |
+——+——+
1 row in set (0.00 sec)
更改表的某一行update
mysql表里存放的數(shù)據(jù)支持更改某個(gè)字段。
– 更改:
mysql> update db1.t1 set name='aaa' where id=1; //更改id=1的name為aaa
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from db1.t1;
+——+——+
| id | name |
+——+——+
| 1 | aaa |
+——+——+
1 row in set (0.00 sec)
清空某個(gè)表的數(shù)據(jù)truncate
有時(shí)候我們不想刪除表,只想清空數(shù)據(jù)。
– 清空:
mysql> truncate table db1.t1; //清空db1庫的t1表
Query OK, 0 rows affected (0.01 sec)
mysql> select * from db1.t1;
Empty set (0.00 sec)
刪除表drop table
如果某個(gè)表不需要了,那就直接刪除。
– 刪除表:
mysql> drop table db1.t1; //刪除db1庫的t1表
Query OK, 0 rows affected (0.01 sec)
六、MySql數(shù)據(jù)庫的備份與恢復(fù)
MySql備份mysqldump
[root@localhost ~]# mysqldump -uroot -p'123456' mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
//-u 和 -p 的作用和前面一樣,后面的mysql指的是庫名,然后重定向到一個(gè)文檔里
MySql恢復(fù)
[root@localhost ~]# mysql -uroot -p'123456' mysql < /tmp/mysql.sql //恢復(fù)時(shí)使用的是mysql命令而不是mysqldump。
Warning: Using a password on the command line interface can be insecure.
擴(kuò)展:
mysql5.7 root密碼更改
myisam 和innodb引擎對(duì)比
mysql 配置詳解
mysql調(diào)優(yōu)
同學(xué)分享的親身mysql調(diào)優(yōu)經(jīng)歷
SQL語句教程
什么是事務(wù)?事務(wù)的特性有哪些
根據(jù)binlog恢復(fù)指定時(shí)間段的數(shù)據(jù)
mysql字符集調(diào)整