1. 程式人生 > >mysql設置更改root密碼、連接mysql、常用命令

mysql設置更改root密碼、連接mysql、常用命令

off etc word pass 查看數據庫 connect 反引號 latin1 狀態

設置、更改root用戶密碼

首次使用mysql會提示‘該命令不在’,原因是還沒有將該命令加入環境變量,如果要使用該命令,需要使用其絕對路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統環境變量。

[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/mysql

重啟系統後該變量會失效,若要永久生效,需要將其加入環境變量配置文件:

[root@localhost ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置:
[root@localhost ~]# source /etc/profile
  • 設置密碼

    首次登陸mysql,root用戶沒有密碼,直接登陸

    [root@localhost ~]# mysql -uroot
    //-u指定用戶登錄
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>quit
    Bye
    // quit命令可以退出mysql。

    設置密碼:
    [root@localhost ~]# mysqladmin -uroot password ‘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)
    // 提示登錄被拒絕,需要密碼。

    使用密碼登錄:
    [root@localhost ~]# mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    // -p參數,使用密碼登錄。可以將密碼接在-p後。也可以不在-p後輸入密碼,根據後面的提示信息輸入,這個方法不會暴露用戶密碼,更安全。

註意: 在沒設置root密碼時使用-p參數登錄mysql,會提示輸入密碼,這是直接回車就行。

  • 更改密碼

    當知道用戶密碼時,進行密碼更改:
    [root@localhost ~]# mysqladmin -uroot -p‘123456‘ password ‘654321‘
    Warning: Using a password on the command line interface can be insecure.
    // 警告密碼在命令行輸入,不安全。但是密碼已經修改成功!

    使用舊密碼登錄:
    [root@localhost ~]# mysql -uroot -p123456
    Warning: Using a password on the command line interface can be insecure.
    // 警告密碼在命令行輸入,不安全。
    ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    // 提示登錄信息驗證失敗,密碼錯誤!

    使用新密碼登錄
    [root@localhost ~]# mysql -uroot -p654321
    Warning: Using a password on the command line interface can be insecure.
    // 警告密碼在命令行輸入,不安全。
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>
    // 使用新密碼登錄成功!

  • 密碼重置

    在不記得root密碼時使用,重置密碼。

    編輯配置文件:
    [root@localhost ~]# vim /etc/my.cnf
    [mysqld]
    skip-grant // 忽略授權!
    ......
    // 在mysqld模塊下加入代碼:skip-grant

    重啟mysql服務:
    [root@localhost ~]# /etc/init.d/mysqld restart
    Shutting down MySQL.. SUCCESS!
    Starting MySQL.. SUCCESS!

註意: 完成上面操作之後登錄mysql就不需要密碼了。

登錄mysql:
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
......
mysql>
// 不使用-p參數直接登錄。

切換到mysql庫:
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> select * from user\G;
// 查看用戶的表信息,該表中存放的是用戶相關信息(密碼、授權…)
// G選項的作用是使輸出信息有序顯示,不加該選項,顯示內容會很亂  
mysql> select password from user;
// 查看用戶密碼,顯示結果Wie加密字符串! 

重置密碼:
mysql> update user set password=password(‘112233‘) where user=‘root‘;
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
// 將密碼更改為‘112233’

恢復配置文件:
[root@localhost ~]# vim /etc/my.cnf
// 將之前加入skip-grant那行註釋掉

重啟mysql服務:
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

登錄:
[root@localhost ~]# mysql -uroot -p‘112233‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
......
mysql> 

重置密碼步驟: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登錄-->use mysql-->update user set password=...-->vim /etc/my.cnf-->刪除skip-grant-->mysql restart。

連接mysql

  • 遠程連接

    使用IP和端口號連接

    [root@localhost ~]# mysql -uroot -p‘112233‘ -h127.0.0.1 -P3306
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor. Commands end with ; or \g.
    ......
    mysql>

    // -h=host,指定IP,-P=port,指定端口號

  • 本地連接

    直接可以直接連接或使用socket連接。

    使用socket鏈接:
    [root@localhost ~]# mysql -uroot -p‘112233‘ -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>

    // -S=socket,指定socket。此方法只適用於本地連接。和直接mysql連接一樣。

  • 連接數據後顯示所有數據庫

    [root@localhost ~]# mysql -uroot -p‘112233‘ -e "show databases"

    Warning: Using a password on the command line interface can be insecure.
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | test |
    +--------------------+

    -e 參數後可以跟一條mysql語句。
    // 該方法常用於shell腳本中。

Mysql 常用命令

查看有哪些數據庫:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

切換到mysql庫:
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

Database changed
mysql> 

查看庫裏的表:
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)

查看表裏面的字段:
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(16)                          | NO   | PRI |                       |       |
| Password               | char(41)                          | NO   |     |                       |       |
| Select_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N                     |       |
| Insert_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N                     |       |
 ......
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)

查看表是怎麽創建的
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_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‘,
  ......

  // \G 是讓結果豎排顯示;

查看當前登錄的用戶:
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

查看當前所在的庫:
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

創建庫:
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db1 //切換到db1庫
Database changed

創建表:
mysql> use db1;  
// 先切換到指定庫下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
// 括號中是定義字段及字段格式,使用反引號引起來
Query OK, 0 rows affected (1.51 sec)
// drop table t1,可以刪除表。

查看當前數據庫的版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)
// 數據庫版本:5.6.35

查看數據庫狀態:
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+

查看所有參數:
mysql> show variables\G;  //查看所有參數

查看指定參數
mysql> show variables like ‘max_connect%‘\G;
// like表示匹配;%是通配符

更改參數:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是臨時更改,如果要永久更改,需要編輯配置文件/etc/my.cnf

查看隊列:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  5 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

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

在mysql中 drop 後跟庫或者表名,可以刪除庫或者表。

可以使用 ctrl+l 清屏

mysql的歷史命令在.mysql_history 文件中。

mysql設置更改root密碼、連接mysql、常用命令