設(shè)置mysql更改root密碼等講析

本文主要給大家介紹設(shè)置MySQL更改root密碼等講析,希望可以給大家補(bǔ)充和更新些知識(shí),如有其它問(wèn)題需要了解的可以持續(xù)在創(chuàng)新互聯(lián)行業(yè)資訊里面關(guān)注我的更新文章的。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、比如網(wǎng)站維護(hù)、網(wǎng)站推廣。

設(shè)置、更改root用戶密碼

首次使用mysql會(huì)提示‘該命令不在’,原因是還沒(méi)有將該命令加入環(huán)境變量,如果要使用該命令,需要使用其絕對(duì)路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統(tǒng)環(huán)境變量。

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

重啟系統(tǒng)后該變量會(huì)失效,若要永久生效,需要將其加入環(huán)境變量配置文件:

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

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

    首次登陸mysql,root用戶沒(méi)有密碼,直接登陸

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

    設(shè)置密碼:
    [root@localhost ~]# mysqladmin -uroot password '123456'
    Warning: Using a password on the command line interface can be insecure.
    // 這里并沒(méi)有報(bào)錯(cuò),只是提示說(shuō)密碼在命令行顯示出來(lái)了不×××全。

    不使用密碼登錄:
    [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參數(shù),使用密碼登錄。可以將密碼接在-p后。也可以不在-p后輸入密碼,根據(jù)后面的提示信息輸入,這個(gè)方法不會(huì)暴露用戶密碼,更安全。

  • 設(shè)置mysql更改root密碼等講析

注意:在沒(méi)設(shè)置root密碼時(shí)使用-p參數(shù)登錄mysql,會(huì)提示輸入密碼,這是直接回車(chē)就行。

  • 更改密碼

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

    使用舊密碼登錄:
    [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)
    // 提示登錄信息驗(yàn)證失敗,密碼錯(cuò)誤!

    使用新密碼登錄
    [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密碼時(shí)使用,重置密碼。

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

    重啟mysql服務(wù):
    [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參數(shù)直接登錄。

切換到mysql庫(kù):
mysql> use mysql; // 切換到mysql庫(kù)
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;
// 查看用戶的表信息,該表中存放的是用戶相關(guān)信息(密碼、授權(quán)…)
// G選項(xiàng)的作用是使輸出信息有序顯示,不加該選項(xiàng),顯示內(nèi)容會(huì)很亂  
mysql> select password from user;
// 查看用戶密碼,顯示結(jié)果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’

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

重啟mysql服務(wù):
[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

  • 遠(yuǎn)程連接

    使用IP和端口號(hào)連接

    [root@localhost ~]# mysql -uroot -p'112233' -h227.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,指定端口號(hào)

  • 本地連接

    直接可以直接連接或使用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連接一樣。

  • 連接數(shù)據(jù)后顯示所有數(shù)據(jù)庫(kù)

    [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 參數(shù)后可以跟一條mysql語(yǔ)句。
    // 該方法常用于shell腳本中。

Mysql 常用命令

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

切換到mysql庫(kù):
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> 

查看庫(kù)里的表:
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)

查看表是怎么創(chuàng)建的
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 是讓結(jié)果豎排顯示;

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

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

創(chuàng)建庫(kù):
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庫(kù)
Database changed

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

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

查看數(shù)據(jù)庫(kù)狀態(tài):
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+

查看所有參數(shù):
mysql> show variables\G;  //查看所有參數(shù)

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

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

查看隊(duì)列:
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 后跟庫(kù)或者表名,可以刪除庫(kù)或者表。

可以使用 ctrl+l 清屏

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

看了以上關(guān)于設(shè)置mysql更改root密碼等講析,希望能給大家在實(shí)際運(yùn)用中帶來(lái)一定的幫助。本文由于篇幅有限,難免會(huì)有不足和需要補(bǔ)充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時(shí)售前售后,隨時(shí)幫您解答問(wèn)題的。

 

文章題目:設(shè)置mysql更改root密碼等講析
轉(zhuǎn)載源于:http://muchs.cn/article16/jcpodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)搜索引擎優(yōu)化、網(wǎng)站策劃網(wǎng)站導(dǎo)航、ChatGPT、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)