1. 程式人生 > >設定mysql使用者密碼、遠端連線資料庫、常用命令

設定mysql使用者密碼、遠端連線資料庫、常用命令

MySQL常用操作

MySQL版本 5.6.35

設定、更改root使用者密碼

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

[[email protected] ~]# exprt PATH=$PATH:/usr/local/mysql/bin/

至此,mysql命令路徑暫時加入環境變數,系統重啟後該變數會失效,若要永久生效,需要將其加入環境變數配置檔案:

[[email protected] ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/
重新整理配置檔案(否則不生效): [[email protected] ~]# source /etc/profile

設定 & 更改密碼

首次登陸mysql,root使用者沒有密碼,直接登入:

[[email protected] ~]# mysql -uroot
#-u:=user,指定使用者名稱
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit
#退出

說明: 登入mysql之後可以進行與mysql相關的一些操作,但是設定mysql使用者的密碼需要執行以下操作!

設定密碼

[[email protected] ~]# mysqladmin -uroot password '123456'  

再次登入:
[[email protected] ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

說明: 設定密碼後直接登入會報錯(ERROR),需要輸入密碼登入。

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

注: -p=passwd,使用密碼登入,在此可以將密碼直接輸入在命令列(跟在-p後面,不加空格:-p'123456'<此處單引號可以不加,但是當密碼中有特殊符號時必須加,所以在命令列輸入密碼時養成習慣:加單引號>),也可以不在命令列輸入,只跟-p選項,然後根據提示資訊:“Enter password”,輸入密碼進行登入(此方法不會暴露使用者密碼,安全)。

更改密碼

  • 當知道使用者密碼時,進行密碼更改:
[[email protected] ~]# mysqladmin -uroot -p'123456' password '1234567'

[[email protected] ~]# mysql -uroot -p'1234567'
Welcome to the MySQL monitor.
mysql>

更改成功!

  • 忘記密碼時,進行密碼更改:
先編輯mysql配置檔案:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授權!
datadir=/data/mysql
socket=/tmp/mysql.sock

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

說明: 完成該操作之後就可以任意登入mysql了(無需密碼),所以此時mysql安全性很差,平時配置檔案中一定不要新增該引數!!!

[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor.  
mysql> use mysql;
#切換mysql庫
Database changed
mysql> select * from user\G;
#檢視使用者的表資訊,該表中存放的是使用者相關資訊(密碼、授權…)
#G選項的作用是使輸出資訊有序顯示,不加該選項,顯示內容會很亂  
mysql> select password from user;
#檢視使用者密碼,顯示結果Wie加密字串!  
mysql> update user set password=password('123456') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
#將密碼更改為‘123456’
mysql> quit
Bye

密碼更改成功!

恢復配置檔案:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

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

登入:
[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit
Bye

Finished!
步驟: vim /etc/my.cnf-->新增skip-grant-->mysql restart-->登入-->use mysql-->update user set password=...-->vim /etc/my.cnf-->刪除skip-grant-->mysql restart。

連線mysql(本地、遠端)

遠端連線:使用IP/port連線

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -h:=host,指定IP;-P:=port,指定埠。

本地連線:使用socket連線

[[email protected] ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -S:=socket,指定socket。此方法只適用於本地連線,等同於“mysql -uroot -p123456”。

顯示所有資料庫

[[email protected] ~]# mysql -uroot -p'123456' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

注: 該方法使用於shell指令碼中。

 MySQL常用命令

檢視庫資訊:


以下命令需要在切換庫(use mysql)之後執行:


注: 以上命令均需要在mysql下執行;在mysql中每行命令末尾加上分號,表示該行命令執行結束。 tb_name即table name()表名。

示例:

[[email protected] mysql]# mysql -uroot -p'123456'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| time_zone                 |
| time_zone_leap_second     |
+---------------------------+
28 rows in set (0.00 sec)

mysql> desc time_zone;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| Time_zone_id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Use_leap_seconds | enum('Y','N')    | NO   |     | N       |                |
+------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)

mysql> show create table time_zone\G;
#G=grep篩選文字內容,規律顯示出來
*************************** 1. row ***************************
       Table: time_zone
Create Table: CREATE TABLE `time_zone` (
  `Time_zone_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Use_leap_seconds` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Time_zone_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Time zones'
1 row in set (0.03 sec)

ERROR: 
No query specified

mysql> select user();
+----------------+
| user()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.07 sec)

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

mysql> select * from user\G;
建立庫:
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)

建立表:
mysql> use db1;  
#先切換到指定庫下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
#括號中是定義欄位及欄位格式,使用反引號引起來
Query OK, 0 rows affected (1.51 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.06 sec)

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)
#在此只是臨時更改,如果要永久更改,需要編輯配置檔案

檢視佇列:
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> drop table t1;
Query OK, 0 rows affected (0.32 sec)

mysql> drop database db1;
Query OK, 0 rows affected (0.10 sec)

MySQL5.7之更改root密碼

與MySQL 5.6版本不同,在安裝MySQL 5.7過程中(初始化)會自動生成root使用者密碼(隨機),那麼在安裝完成後如何更改root使用者密碼?步驟如下:

檢視預設密碼

[[email protected] mysql]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcFP

更改root密碼:已知預設密碼

使用預設密碼登入:
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot -p'3A)2DdJLkcFP'
Welcome to the MySQL monitor.  
Your MySQL connection id is 3
Server version: 5.7.17

設定新密碼:
方法1:
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
方法2:
mysql> SET PASSWORD FOR 'root'@localhost = PASSWORD('123456');
mysql> quit
Bye

Finished!

更改root密碼:不知道預設密碼

編輯配置檔案:
[[email protected] mysql]# vi /etc/my.cnf

[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加引數:skip-grant-tables

重啟:  
[[email protected] mysql]# /etc/init.d/mysqld restart

登入:此時不需要密碼
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot 

更改密碼:
mysql> update user set authentication_string=password('12456') where user='root';
mysql>quit

[[email protected] mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重啟: 
[[email protected] mysql]# /etc/init.d/mysqld restart
 擴充套件 
mysql5.7 root密碼更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎對比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置詳解: http://blog.linuxeye.com/379.html
mysql調優: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同學分享的親身mysql調優經歷: http://www.apelearn.com/bbs/thread-11281-1-1.html