MySQL的基本操做mysql
1、忘記root密碼sql
1、編輯mysql主配置文件 my.cnf,配置文件路徑:/etc/my.cnfshell
[root@mysql ~]# vim /etc/my.cnf 數據庫
而後再配置文件中找到[mysqld]字段,在[mysqld]字段下添加參數skip-grant ,以下圖:vim
退出保存安全
重啓mysql服務bash
[root@mysql ~]# /etc/init.d/mysqld restart Shutting down MySQL.... SUCCESS! #這個提示就表示MySQL已經啓動起來
2、重啓完成以後使用mysql -uroot就能夠不用輸入密碼直接登陸MySQL服務器服務器
可是登錄的時候報錯信息以下:網絡
[root@mysql ~]#mysql -uroot -bash: mysql:command not found
解決辦法是執行有三個:tcp
A、先更改PATH,容許mysql登陸,可是這樣子的作法呢,就是機器重啓以後你登陸的時候
仍是會報錯,還須要執行這個命令,另一個辦法就是把這個命令加入到開啓啓動文件呢中
去。
[root@mysql ~]# PATH=$PATH:/usr/local/mysql/bin
加入到開啓動文件中去,添加到最後一行,如圖所示
開機啓動配置文件:/etc/rc.local
B、作軟鏈接接
以上A、B兩種方法任何一個均可以
系統重啓以後,須要輸入:[root@mysql ~]# source /etc/rc.local 命令而後就可使用密碼登陸了
如今能夠登陸mysql,不須要輸入密碼,直接能夠登陸
[root@mysql ~]#mysql -uroot elcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.40 MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
3、更改密碼
首先use mysql進去這個庫
mysql> use mysql
Database changed
而後更新
mysql> update user set password=password('123456') where user='root'; Query OK, 2 rows affected (0.04 sec) Rows matched: 3 Changed: 2 Warnings: 0
這個命令的意思就是更新user表,修改密碼爲123456,全部的root都更改
4、密碼更改完成以後須要刷新表,刷新或者重啓均可以
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
5、密碼修改完成以後,須要在/etc/my.cnf文件中把剛開始添加的skip-gran字段刪除
否則mysql沒有安全性
而後重啓mysql服務
[root@mysql ~]#/etc/init.d/mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL...SUCCESS!
六、在此登陸mysql的時候須要輸入密碼才能夠登陸
不輸入密碼報錯
[root@mysql ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
輸入密碼登陸
[root@mysql ~]#mysql -uroot -p654321 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.40 MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
-p:後面跟的是密碼,這是登陸方式
7、直接在shell中執行mysql的命令。須要加上-e參數以下圖:
[root@mysql ~]# mysql -uroot -p654321 -e "use mysql; select user,host from user where user='root'"
8、使用tcp來鏈接數據庫
[root@mysql ~]# mysql -uroot -p654321 -h127.0.0.1 -P3306
-h:指定遠程主機的ip
-P(大寫的):用來指定遠程主機mysql的綁定端口
2、mysql經常使用操做
1、查看都有哪些庫 show databases;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cheng | | mysql | | test | +--------------------+ 4 rows in set (0.04 sec)
information_schema、mysql、test這三個表示默認都存在的
結束的標誌是以封號(;)來結尾的
2、查看某個庫的表
先進入一個庫
mysql> use cheng
Database changed
查看cheng這個庫裏都有哪些表
mysql> show tables;
3、查看錶的字段 desc tb;
mysql> desc pre_common_block;
desc後面跟表名
3、查看建表語句 show create table tb;
mysql> show create table pre_common_block;
使用G能夠分割
mysql> show create table pre_common_block\G;
4、當前是哪一個用戶 select user();
mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
5、當前庫 select database();
mysql> select database();
+------------+
| database() |
+------------+
| cheng |
+------------+
1 row in set (0.00 sec)
6、建立庫 create database db1;
mysql> create database cheng2;
Query OK, 1 row affected (0.02 sec)
7、建立表 create table t1 (`id` int(4), `name` char(40));
進入剛纔建立的庫
mysql> use cheng2;
Database changed
建立表
mysql> create table `st` (`id` int(4),`name` char(50)) ENGINE=MYISAM DEFAULT CHARSET=gbk; Query OK, 0 rows affected (0.13 sec)
Init:類型
Char:字符串
ENGINE=MYISAM DEFAULT CHARSET=gbk:默認引擎
查詢剛纔建立的st表的語句
mysql> show create table st\G; *************************** 1. row *************************** Table: st Create Table: CREATE TABLE `st` ( `id` int(4) DEFAULT NULL, `name` char(50) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=gbk 1 row in set (0.00 sec) ERROR: No query specified ERROR: No query specified
查看庫裏面有幾個表
mysql> show tables;
+------------------+
| Tables_in_cheng2 |
+------------------+
| st |
+------------------+
1 row in set (0.00 sec)
8、查看數據庫版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.40 |
+-----------+
1 row in set (0.00 sec)
9、更改表的某一行 update db1.t1 set name='aaa' where id=1
mysql> update st set name='ct' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看更改過的結果
mysql> select * from st;
+------+------+
| id | name |
+------+------+
| 1 | ct |
+------+------+
1 row in set (0.00 sec)
10、插入新的一行
mysql> insert into st(id,name) values(1,'cheng168');
Query OK, 1 row affected (0.01 sec)
查詢表
mysql> select * from st;
+------+----------+
| id | name |
+------+----------+
| 1 | cheng168 |
+------+----------+
1 row in set (0.00 sec)
11、刪除表中的一行
mysql> delete from st where name='ct'
Query OK, 1 row affected (0.01 sec)
查看st表,發現表中的一行已經不存在了,成了空表
mysql> select * from st;
Empty set (0.00 sec)
12、刪除一個表中的全部行
mysql> truncate table st;
Query OK, 0 rows affected (0.00 sec)
查看
mysql> select * from st;
Empty set (0.00 sec)
清空表 truncate table db1.t1;
13、刪除表 drop table db1.t1;
mysql> drop table st;
Query OK, 0 rows affected (0.08 sec)
14、刪除數據庫 drop database db1;
mysql> drop database cheng2;
Query OK, 0 rows affected (0.16 sec)
在Linux系統中,使用cat .mysql_history能夠查看mysql的歷史命令
[root@mysql ~]# cat .mysql_history
15、建立普通用戶並受權 grant all on cheng1.* to 'user1'@'127.0.0.1' identified by '123456';
mysql> grant all on cheng1.* to 'user1'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
all:表示所全部的權限
*.*:前面的*表示的是全部的數據庫,後面的*表示的是全部的表
Identified by:後面跟密碼,要使用單引號括起來
127.0.0.1:表示的是網絡上的主機,固然也能夠%號來表示,使用%號就表示全部
16、查看mysql狀態 show status;
mysql> show status;
17、更改密碼 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user='username' ;
18、 show variables:查看參數
mysql> show variables;
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| Variable_name | Value |
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| autocommit | ON |
| automatic_sp_privileges | ON |
| back_log | 50 |
19、修改mysql參數
過濾出帶有關鍵字的參數:show variables like 'max_connect%';
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
修改參數:set global max_connect_errors = 1000;
mysql> set global max_connect_errors = 1000;
Query OK, 0 rows affected (0.00 sec)
如今查看已經把max_connect_errors參數修改了
mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
+--------------------+-------+
1 row in set (0.00 sec)
set global:臨時修改某些參數,MySQL重啓以後就會還原,永久生效,須要修改my.cnf文件
%:至關於Linux中*的意思,通配的意思
20、查看mysql隊列 show processlist;
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 2 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
21、查詢 select count(*) from mysql.user;
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.01 sec)
Mysql.user:表示的是mysql庫中的user表
count(*):表示的是表中有多少行
22、修復表 repair table tb1 [use frm];
mysql> repair table pre_ucenter_settings;
+----------------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------------------+--------+----------+----------+
| cheng.pre_ucenter_settings | repair | status | OK |
+----------------------------+--------+----------+----------+
1 row in set (0.05 sec)
pre_ucenter_settings:是表名
也能夠在使用repair table pre_ucenter_settings use_frm;來修復
use_frm:
select * from mysql.db; select * from mysql.db where host like '10.0.%';
3、mysql備份與恢復
1、備份數據庫 mysqldump -uroot -p db >1.sql
[root@mysql ~]# mysqldump -uroot -p654321 cheng > /tmp/mysqbackup.sql
cheng:數據庫名稱,就是你要備份哪個數據庫
/tmp/mysqbackup.sql:備份到/tmp/目錄下,而且命名爲mysqbackup.sql
2、恢復 mysql -uroot -p db <1.sql
[root@mysql ~]# mysql -uroot -p654321 cheng < /tmp/mysqbackup.sql
3、只備份一個表 mysqldump -uroot -p db tb1 > 2.sql
恢復的時候跟恢復數據時同樣的,不須要指定表名
4、備份時指定字符集 mysqldump -uroot -p --default-character-set=gbk db >1.sql
--default-character-set=gbk:指定字符集
5、 恢復也指定字符集 mysql -uroot -p --default-character-set=utf8 db < 1.sql
6、只備份建表語句
[root@mysql ~]# mysqldump -uroot -p654321 -d cheng pre_forum_post> /tmp/post.sql
-d:只備份建表語句
有錯誤的地方懇請大神指正,小白會感激涕零同時也會繼續修改