54:mysql修改密碼|鏈接mysql|mysql經常使用命令

一、mysql修改密碼html

root用戶時mysql的超級管理員,默認mysql的密碼是空的,直接能夠鏈接上去的,不過這樣不安全;mysql

註釋:爲了方便的使用mysql,須要把mysql加入到環境變量裏;     #後續本身輸入mysql能夠進入到mysql下;linux

(1):啓動mysql後,使用mysql   -uroot命令,會提示mysql命令不存在,這是由於安裝的mysql是在/usr/local/mysql/bin/mysql,而這個目錄不在環境變量PATH裏面,因此會報錯,若想要這個命令直接運行,須要把PATH作一個更改;web

[root@localhost_001 vhost]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost_001 vhost]# export PATH=$PATH:/usr/local/mysql/bin

註釋:export聲明的變量臨時生效,會在重啓後失效;sql

註釋:若想變量永久生效,建議PATH=$PATH:/usr/local/mysql/bin寫入到/etc/profile文件裏,而後執行  source   /etc/profile數據庫

(2):進入mysql,   使用mysql  -uroot  就能夠進入mysql,退出mysql使用quit或者exit便可;vim

[root@localhost_001 vhost]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> exit
Bye

註釋:登陸mysql通常使用 mysql   -uroot  -p   (-p表示指定密碼):密碼爲空時,直接回車也可進入到mysql;安全

(3):設置mysql的登陸密碼;(當前mysql沒有密碼的狀況下設置);           mysqladminbash

[root@localhost_001 ~]# mysqladmin -uroot password 'nihao123!'
Warning: Using a password on the command line interface can be insecure.

註釋:提示是警告信息,是說你如今的命令在當前的命令行顯示出來了,這樣不太安全;socket

登陸mysql;

[root@localhost_001 ~]# mysqladmin -uroot password 'nihao123!'       #要求密碼登陸;
Warning: Using a password on the command line interface can be insecure.
[root@localhost_001 ~]# mysql -uroot 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost_001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

註釋:當設置的密碼以後,再次登陸時會提示輸入密碼才能登陸,如上圖;

(4):在知道mysql的密碼的狀況下,去修改mysql的密碼;

[root@localhost_001 ~]# mysqladmin -uroot -p'nihao123!' password 'nihao123@'
Warning: Using a password on the command line interface can be insecure.

註釋:如上格式:  mysqladmin    -uroot   -p'舊密碼'  password  '新密碼'

註釋在明文指定密碼的時候,密碼能夠加單引號,也能夠不加單引號,建議加上單引號,防止密碼有特殊字符的存在(如果不加單引號,而密碼中又有中文字符,則會不識別);

註釋:指定新密碼登陸,固然也能夠不明文登陸,  -p回車輸入密碼也能夠;

[root@localhost_001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

(5):忘記mysql密碼的時候修改mysql的密碼;

首先要去mysql的配置文件/etc/my.cnf里加入skip-grant;而後重啓mysql服務,再次進入mysql則不須要密碼;

skip-grant 表示忽略受權,也就是登陸mysql的時候不須要輸入密碼,能夠直接登陸;

[root@localhost_001 ~]# vim  /etc/my.cnf
[mysqld]
skip-grant
datadir=/data/mysql
socket=/tmp/mysql.sock
[root@localhost_001 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

(1):登陸mysql,在登陸mysql後,還須要更改一個user表(這個表裏存放的用戶名和密碼),這個表在mysql庫裏面;(進入到這個庫)

[root@localhost_001 ~]# mysql -uroot        #進入mysql;
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> use mysql;                           #切換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>

(2):查看user表和password表相關信息;

     select * from user;    ----->   查看user表,會看到不少的亂碼信息,這裏保存的mysql的用戶名和密碼等信息;

     select password from user user='root';      針對root用戶查看password表;

mysql> select password from user where user='root';
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *FA22FC2D09A3E4C4F63AAE8F4AE18DD2F3E7E695 |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
4 rows in set (0.00 sec)

(3):更改mysql的密碼; 

update   user  set   password=('111111')   where  user='root';

                              密碼字段 函數 | 用於加密密碼 |爲條件語句

mysql> update user set password=password('111111') where user='root';
Query OK, 4 rows affected (0.08 sec)
Rows matched: 4  Changed: 4  Warnings: 0

註釋:提示說4行則表示密碼更改完成;可按以下方式驗證;

先去my.cnf的配置文件了註釋掉skip-grant(受權登陸);(若是不刪除,那麼全部的用戶都不須要輸入密碼就能夠登陸,這樣不安全);

重啓mysql服務;  service   msyqld restart

而後用新密碼登陸驗證;      mysql   -uroot  -p111111   

這樣即成功修改可mysql的密碼;

鏈接mysql的方式,   本地鏈接      or       遠程鏈接

本地鏈接:即便沒有使用-S來指定socket,默認也是使用/tmp/mysql.sock鏈接;

mysql  -uroot  -p11111            <==========>      mysql   -uroot   -p11111  -S/tmp/mysql.sock

[root@localhost_001 ~]# mysql -uroot -p111111
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
[root@localhost_001 ~]# mysql -uroot -p111111 -S/tmp/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql>

註釋:socket方式只能使用本地鏈接使用;

遠程鏈接:  mysql  -uroot     -p密碼    -h[遠端mysql主機IP地址]    -P[遠端mysql端口號]         #無前後順序;

[root@localhost_001 ~]# mysql  -uroot  -p111111 -h127.0.0.1 -P3306
Warning: Using a password on the command line interface can be insecure.
mysql> exit
Bye

假設咱們從A機器(192.168.149.130)鏈接B機器(192.168.149.129)的mysql數據庫;

[root@localhost_002 ~]# mysql -h192.168.149.129 -P3306 -p111111
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'192.168.149.130' (using password: YES)

註釋:如上鍊接時提示錯誤;由於mysql默認是隻容許127.0.0.1和localhost登陸,因此須要添加容許192.168.149.130受權登陸;

B機器(192.168.149.129)執行以下;

mysql> grant all privileges on *.* TO 'root'@'192.168.149.130' identified by 'nihao123!' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;                #刷新權限;
Query OK, 0 rows affected (0.00 sec)

A機器(192.168.149.130)遠程鏈接便可;

[root@localhost_002 ~]# mysql -h192.168.149.129 -P3306 -pnihao123!
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

註釋:在B機器(192.168.149.129)查看受權的用戶;

mysql> select host from user where user='root';
+-----------------+
| host            |
+-----------------+
| 127.0.0.1       |   
| 192.168.149.130 |
| ::1             |
| localhost       |
| localhost\_001  |
+-----------------+
6 rows in set (0.00 sec)

註釋:目前的只容許本地和遠程主機130登陸,其他主機都拒絕;

註釋:通常爲了安全性,mysql主機是本地登陸的;

註釋:固然若是想讓全部用戶都鏈接mysql,能夠以下設置;

mysql> grant all privileges on *.* TO 'root'@'%' identified by 'nihao123!' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;                #刷新權限;
Query OK, 0 rows affected (0.00 sec)

1:鏈接mysql後遠程執行命令後退出;   mysql  -uroot  -p111111  -e "show databases;"

[root@localhost_001 ~]# mysql -uroot -p111111 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

遠程鏈接執行命令;   mysql   -h192.168.149.129  -uroot  -pnihao123!  -P3306    -e  「show   databases;」

[root@localhost_002 ~]# mysql -h192.168.149.129 -uroot -pnihao123! -P3306 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

mysql經常使用命令

查詢庫 show databases;
切換庫 use mysql;
查看庫裏的表 show tables;
查看錶裏的字段 desc tb_name;
查看建表語句 show create table tb_name\G;
查看當前用戶 select user();
查看當前使用的數據庫 select database();
建立庫 create database db1;
建立表 use db1; create table t1(id int(4), name char(40));
查看當前數據庫版本 select version();
查看數據庫狀態 show status;
查看各參數 show variables; show variables like 'max_connect%';
修改參數 set global max_connect_errors=1000;
查看隊列 show processlist; show full processlist;

1:建立庫;  create    databases  db1;

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

2:查看有哪些庫;  show   databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

3:切換庫;    use    mysql;

mysql> use db1;
Database changed

4:建立表; create  tables    後面加字段;

mysql> create table t1(`id` int(4),  `name`  char(40));
Query OK, 0 rows affected (0.03 sec)

5:查看錶裏的字段;    show   create  table  t1\G;             desc  user;      ------->   查看user表有哪些字段;

mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified
mysql> desc t1\G
*************************** 1. row ***************************
  Field: id
   Type: int(4)
   Null: YES
    Key: 
Default: NULL
  Extra: 
*************************** 2. row ***************************
  Field: name
   Type: char(40)
   Null: YES
    Key: 
Default: NULL
  Extra: 
2 rows in set (0.00 sec)

註釋:在命令前面加#號則表示取消這個命令;

刪除一個表;     drop    table    t1;

mysql> drop table t1;
Query OK, 0 rows affected (0.03 sec)

從新建立一個表,並指定字符集爲utf-8;

mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT  CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

再次查看會發現字符集變成了utf-8;

mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified

6:查看噹噹前所在庫;    select    database();

mysql> select database();
+------------+
| database() |
+------------+
| db1        |
+------------+
1 row in set (0.00 sec)

7:查看當前登陸數據庫的用戶;     select     user();

mysql> select user();
+--------------------+
| user()             |
+--------------------+
| root@localhost_001 |
+--------------------+
1 row in set (0.00 sec)

8:查看當前數據庫的版本;    select    version();

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.39    |
+-----------+
1 row in set (0.00 sec)

   show   status;      列出mysql的數據庫狀態,會把經常使用數據都列出來;

9:查看mysql的各個參數(在/etc/my.cnf定義的參數);     

查看mysql定義各個參數:         show     variables;

查看指定的參數,用like;            show     variables    like   'max_connect%';

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

10:修改參數,不過僅在內存中生效;      set    global    max_connect_errors=1000;

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

註釋:如果想從新後生效,能夠修改/etc/my.cnf配置文件;

11:查看隊列;     show    processlist;       用來查看有哪些用戶在連mysql,在作什麼操做,是否有鎖表;    

mysql> show processlist;
+----+------+---------------------+------+---------+------+-------+------------------+
| Id | User | Host                | db   | Command | Time | State | Info             |
+----+------+---------------------+------+---------+------+-------+------------------+
|  6 | root | localhost_001:55484 | db1  | Query   |    0 | init  | show processlist |
+----+------+---------------------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

12: show   full  processlist;        #同查看隊列命令,不會最後一列會較詳細的顯示出來;

mysql> show full processlist;
+----+------+---------------------+------+---------+------+-------+-----------------------+
| Id | User | Host                | db   | Command | Time | State | Info                  |
+----+------+---------------------+------+---------+------+-------+-----------------------+
|  6 | root | localhost_001:55484 | db1  | Query   |    0 | init  | show full processlist |
+----+------+---------------------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

註釋:

mysql5.7 root密碼更改 http://www.apelearn.com/bbs/thread-7289-1-1.html myisam 和innodb引擎對比 http://www.pureweber.com/article/myisam-vs-innodb/ 知乎上的答案 https://www.zhihu.com/question/20596402 mysql 配置詳解:https://www.jb51.net/article/48082.htm mysql調優: http://www.aminglinux.com/bbs/thread-5758-1-1.html 同窗分享的親身mysql調優經歷: http://www.apelearn.com/bbs/thread-11281-1-1.html

相關文章
相關標籤/搜索