設置mysql用戶密碼(5.6/5.7)、遠程連接數據庫、常用命令
MySQL版本 5.6.35
13.1 設置、更改root用戶密碼
首次直接使用mysql會提示‘該命令不存在’,原因是還沒有將該命令加入環境變量,如果要使用該命令,需要使用其絕對路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統環境變量:
[root@centos-01inux ~]# exprt PATH=$PATH:/usr/local/mysql/bin/
至此,mysql命令路徑暫時加入環境變量,系統重啟後該變量會失效,若要永久生效,需要將其加入環境變量配置文件:
[root@centos-01inux ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/
刷新配置文件(否則不生效):
[root@centos-01inux ~]# source /etc/profile
設置 & 更改密碼
首次登陸mysql,root用戶沒有密碼,直接登錄:
[root@centos-01inux ~]# mysql -uroot
#-u:=user,指定用戶名
Welcome to the MySQL monitor. Commands end with ; or \g.
……
mysql> quit
#退出
說明: 登錄mysql之後可以進行與mysql相關的一些操作,但是設置mysql用戶的密碼需要執行以下操作!
設置密碼
[root@centos-01inux ~]# mysqladmin -uroot password ‘123456‘
再次登錄:
[root@centos-01inux ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
說明: 設置密碼後直接登錄會報錯(ERROR),需要輸入密碼登錄。
[root@centos-01inux ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.
mysql>
註: -p=passwd,使用密碼登錄,在此可以將密碼直接輸入在命令行(跟在-p後面,不加空格:-p‘123456‘<此處單引號可以不加,但是當密碼中有特殊符號時必須加,所以在命令行輸入密碼時養成習慣:加單引號>),也可以不在命令行輸入,只跟-p選項,然後根據提示信息:“Enter password”,輸入密碼進行登錄(此方法不會暴露用戶密碼,安全)。
更改密碼
當知道用戶密碼時,進行密碼更改:
[root@centos-01inux ~]# mysqladmin -uroot -p‘123456‘ password ‘1234567‘
[root@centos-01inux ~]# mysql -uroot -p‘1234567‘
Welcome to the MySQL monitor.
mysql>
更改成功!
忘記密碼時,進行密碼更改:
先編輯mysql配置文件:
[root@centos-01inux ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授權!
datadir=/data/mysql
socket=/tmp/mysql.sock
重啟mysql服務:
[root@centos-01inux ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL..................... SUCCESS!
說明: 完成該操作之後就可以任意登錄mysql了(無需密碼),所以此時mysql安全性很差,平時配置文件中一定不要添加該參數!!!
[root@centos-01inux ~]# 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
密碼更改成功!
恢復配置文件:
[root@centos-01inux ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
重啟mysql服務:
[root@centos-01inux ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL........... SUCCESS!
登錄:
[root@centos-01inux ~]# 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。
13.2 連接mysql(本地、遠程)
遠程連接:使用IP/port連接
[root@centos-01inux ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye
註: -h:=host,指定IP;-P:=port,指定端口。
本地連接:使用socket連接
[root@centos-01inux ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye
註: -S:=socket,指定socket。此方法只適用於本地連接,等同於“mysql -uroot -p123456”。
顯示所有數據庫
[root@centos-01inux ~]# 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腳本中。
13.3 MySQL常用命令
查看庫信息:
mark
以下命令需要在切換庫(use mysql)之後執行:
mark
編輯庫:
mark
註: 以上命令均需要在mysql下執行;在mysql中每行命令末尾加上分號,表示該行命令執行結束。 tb_name即table name()表名。
示例:
[root@centos-01inux 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() |
+----------------+
| root@localhost |
+----------------+
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用戶密碼?步驟如下:
查看默認密碼
[root@centos-01inux 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密碼:已知默認密碼
使用默認密碼登錄:
[root@centos-01inux 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密碼:不知道默認密碼
編輯配置文件:
[root@centos-01inux mysql]# vi /etc/my.cnf
[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加參數:skip-grant-tables
重啟:
[root@centos-01inux mysql]# /etc/init.d/mysqld restart
登錄:此時不需要密碼
[root@centos-01inux mysql]# /usr/local/mysql/bin/mysql -uroot
更改密碼:
mysql> update user set authentication_string=password(‘12456‘) where user=‘root‘;
mysql>quit
[root@centos-01inux mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
重啟:
[root@centos-01inux mysql]# /etc/init.d/mysqld restart
設置mysql用戶密碼(5.6/5.7)、遠程連接數據庫、常用命令