mysql(設置/更改mysql密碼,連接MySQL,MySQL常用命令,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:
Welcome to the MySQL monitor. Commands end with ; or \g.
二、如何更改MySQL密碼
[root@lnmp ~]# mysqladmin -uroot -p'westos123' password 'westos321' (這裏需要註意-p後不用加空格)
Warning: Using a password on the command line interface can be insecure.
[root@lnmp ~]# mysql -uroot -p'westos123' (用舊密碼登錄時報錯)
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@lnmp ~]# mysql -uroot -p'westos321' (新密碼可以正常登錄)
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
三、如何重置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 |
| *1836D7557E753782F1509748BD403456701A0D2F |
| *1836D7557E753782F1509748BD403456701A0D2F |
| *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
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
我們再嘗試用新密碼登錄,發現配置完成、
[root@lnmp ~]# mysql -uroot -p'aminglinux'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
連接MySQL
一、連接mysql的二種方法。
第一種就是我們經常訪問本機的方式,直接使用賬戶和密碼登錄
[root@lnmp ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
其實這種方法,默認是用sock的連接的,如果把命令全部敲出來,應該是
[root@lnmp ~]# mysql -uroot -paminglinux -S/tmp/mysql.sock (用S來指定sock文件,默認監聽的是/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來做實驗,本機的ip是127.0.0.1)
[root@lnmp ~]# mysql -uroot -paminglinux -h127.0.0.1 -P3306 (-h來指定ip地址,-P指定端口號,MySQL默認外網訪問3306端口)
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
二、用命令行直接操作mysql命令。(在shell腳本時非常實用)
[root@lnmp ~]# mysql -uroot -paminglinux -e "show databases" (-e直接加命令,可以不用進入mysql直接操作,查看所有數據庫)
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
MySQL常用命令
mysql> create database rxr; (創建一個rxr的數據庫)
Query OK, 1 row affected (0.49 sec)
mysql> show databases; (查看本地所有數據庫)
+--------------------+
| Database |
+--------------------+
| information_schema |
| lty |
| mysql |
| performance_schema |
| rxr |
| test |
+--------------------+
6 rows in set (0.00 sec)
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 |
+-------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
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 |
+----------------+
1 row in set (0.00 sec)
mysql> select database(); (查看當前使用的數據庫)
+------------+
| database() |
+------------+
| rxr |
+------------+
1 row in set (0.00 sec)
mysql> select (); (查看當前數據庫版本)
+-----------+
| version() |
+-----------+
| 5.6.36 |
+-----------+
1 row in set (0.00 sec)
mysql> show status; (查看數據庫狀態)因為比較長,所以就不列出來內容了
mysql> show variables; (查看各項參數,一般這裏的參數都可以用vim在/etc/my.cnf裏修改)因為比較長,所以就不列出來內容了mysql> show variables like 'max_connect_errors'; (列出來指定選項)
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
+--------------------+-------+
1 row in set (0.00 sec)
mysql> set global max_connect_errors=1000; (直接在數據庫裏修改參數,但是如果想要永久修改,還是要去配置文件裏)
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like 'max_connect_errors'; (可以看到max_connect_errors被修改成1000)
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
+--------------------+-------+
1 row in set (0.00 sec)
mysql> show processlist; (這個命令相當於ps或者top,查看數據庫的操作)
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 3 | root | localhost | rxr | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
MySQL引擎myisam和innodb的區別:
http://blog.csdn.net/xifeijian/article/details/20316775
mysql(設置/更改mysql密碼,連接MySQL,MySQL常用命令,MySQL兩種引擎區別)