設定/更改MySQL的密碼問題
一,設定mysql密碼
我們安裝MySQL時,把它放在了/usr/local/mysql/下,在目前的環境中并沒有這個目錄,是以我們要把目錄添加到目前目錄下。
[root@lnmp ~]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
[root@lnmp ~]# source /etc/profile
這樣我們就可以在任意環境下進入MySQL,第一次進入MySQL時,是沒有密碼的。
[root@lnmp ~]# mysql -uroot (用這個指令可以直接進入mysql。)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
mysql> quit (quit可以直接退出MySQL)
Bye
[root@lnmp ~]# ll /usr/local/mysql/bin/mysqladmin (在Mysql/bin下,這個檔案用來配置或更改mysql密碼)
-rwxr-xr-x. 1 7161 31415 8055556 3月 18 2017 /usr/local/mysql/bin/mysqladmin
[root@lnmp ~]# mysqladmin -uroot password 'westos123'; (添加密碼westos123,下面warning是警告你把密碼顯示出來了)
Warning: Using a password on the command line interface can be insecure.
我們不輸入密碼登入看看:
[root@lnmp ~]# mysql -uroot (提示你沒有輸入密碼)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@lnmp ~]# mysql -uroot -p (加-p輸入剛才的密碼即可進入)
Enter password:
二、如何更改MySQL密碼
[root@lnmp ~]# mysqladmin -uroot -p'westos123' password 'westos321' (這裡需要注意-p後不用加空格)
[root@lnmp ~]# mysql -uroot -p'westos123' (用舊密碼登入時報錯)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@lnmp ~]# mysql -uroot -p'westos321' (新密碼可以正常登入)
三、如何重置MySQL密碼?(忘記了MySQL密碼)
[root@lnmp ~]# vim /etc/my.cnf (修改配置檔案)
[mysqld] (在mysqld的下面加上一行)
skip-grant
[root@lnmp ~]# /etc/init.d/mysqld restart (重新啟動MySQL服務)
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
進入mysql庫裡更改一個表。
mysql> select password from user; (可以檢視密碼)
+-------------------------------------------+
| password |
| *1836D7557E753782F1509748BD403456701A0D2F |
| |
6 rows in set (0.00 sec)
下面這條指令就是用來修改密碼的,第一個password是字元,第二個password是函數,我們看到上面的密碼都是加密的,就是因為函數
mysql> update user set password=password('aminglinux') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
修改完配置檔案以後,我們應該把剛才的配置檔案改回來,并重新啟動MySQL,否則任何人登入MySQL都不用密碼。
[root@lnmp ~]# vim /etc/my.cnf
删除 skip-grant
[root@lnmp ~]# /etc/init.d/mysqld restart
Starting MySQL. SUCCESS!
我們再嘗試用新密碼登入,發現配置完成、
[root@lnmp ~]# mysql -uroot -p'aminglinux'
連接配接MySQL
一、連接配接mysql的二種方法。
第一種就是我們經常通路本機的方式,直接使用賬戶和密碼登入
[root@lnmp ~]# mysql -uroot -paminglinux
其實這種方法,預設是用sock的連接配接的,如果把指令全部敲出來,應該是
[root@lnmp ~]# mysql -uroot -paminglinux -S/tmp/mysql.sock (用S來指定sock檔案,預設監聽的是/tmp/mysql.sock)
遠端連接配接:(還是用本機的MySQL來做實驗,本機的ip是127.0.0.1)
[root@lnmp ~]# mysql -uroot -paminglinux -h127.0.0.1 -P3306 (-h來指定ip位址,-P指定端口号,MySQL預設外網通路3306端口)
二、用指令行直接操作mysql指令。(在shell腳本時非常實用)
[root@lnmp ~]# mysql -uroot -paminglinux -e "show databases" (-e直接加指令,可以不用進入mysql直接操作,檢視所有資料庫)
+--------------------+
| Database |
| information_schema |
| mysql |
| performance_schema |
| test |
MySQL常用指令
mysql> create database rxr; (建立一個rxr的資料庫)
Query OK, 1 row affected (0.49 sec)
mysql> show databases; (檢視本地所有資料庫)
| lty |
| rxr |
mysql> use rxr; (進入到rxr資料庫中)
Database changed
mysql> create table lty(`id`int(4),`name`char(40)); (建立一個lty的表,Id和Name隻是表裡的參數,括号裡定義最大字元)
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; (檢視所在資料庫裡的表)
+---------------+
| Tables_in_rxr |
| lty |
1 row in set (0.00 sec)
mysql> show create table lty; (檢視建表的語句)
+-------+------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
| lty | CREATE TABLE `lty` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
mysql> desc lty; (檢視表裡的字段)
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
| id | int(4) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
mysql> select user(); (檢視連結資料庫的使用者)
+----------------+
| user() |
| root@localhost |
mysql> select database(); (檢視目前使用的資料庫)
+------------+
| database() |
| rxr |
mysql> select (); (檢視目前資料庫版本)
+-----------+
| version() |
| 5.6.36 |
mysql> show status; (檢視資料庫狀态)因為比較長,是以就不列出來内容了
mysql> show variables; (檢視各項參數,一般這裡的參數都可以用vim在/etc/my.cnf裡修改)因為比較長,是以就不列出來内容了mysql> show variables like 'max_connect_errors'; (列出來指定選項)
+--------------------+-------+
| Variable_name | Value |
| max_connect_errors | 100 |
mysql> set global max_connect_errors=1000; (直接在資料庫裡修改參數,但是如果想要永久修改,還是要去配置檔案裡)
mysql> show variables like 'max_connect_errors'; (可以看到max_connect_errors被修改成1000)
| max_connect_errors | 1000 |
mysql> show processlist; (這個指令相當于ps或者top,檢視資料庫的操作)
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
| 3 | root | localhost | rxr | Query | 0 | init | show processlist |
MySQL引擎myisam和innodb的差別:
<a href="http://blog.csdn.net/xifeijian/article/details/20316775">http://blog.csdn.net/xifeijian/article/details/20316775</a>
本文轉自 小新銳 51CTO部落格,原文連結:http://blog.51cto.com/13407306/2061274,如需轉載請自行聯系原作者