1. 程式人生 > >mysql的設置更改root密碼、連接、常用命令

mysql的設置更改root密碼、連接、常用命令

cte shel $path ace tor 永久生效 startup for /etc

13.1 設置更改root密碼
  • 更改環境變量PATH ,增加mysql絕對路徑
    首次直接使用mysql會提示‘該命令不存在’,原因是還沒有將該命令加入環境變量,如果要使用該命令,需要使用其絕對路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統環境變量:
[root@taoyuan ~]# export PATH=$PATH:/usr/local/mysql/bin/

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

vi /etc/profile

#在配置文件最後 把上面的命令加

#執行source命令生效
[root@taoyuan ~]# source /etc/profile
  • 首次登陸
[root@taoyuan ~]# mysql -uroot 

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

  • 設置mysql 的root密碼 && 更改
[root@taoyuan ~]# mysqladmin -uroot password ‘123456‘
Warning: Using a password on the command line interface can be insecure.

[root@taoyuan ~]# mysql -uroot -p
Enter password: 
#輸入密碼

#更改新的密碼
[root@taoyuan ~]# mysqladmin -uroot -p‘123456‘ password ‘taoyuan‘
Warning: Using a password on the command line interface can be insecure.
[root@taoyuan ~]# mysql -uroot -p‘taoyuan‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
  • 密碼重置
[root@taoyuan ~]# vi /etc/my.cnf

#my.cnf 配置文件內容
[mysqld]
skip-grant
datadir=/data/mysql #增加skip-grant
#忽略授權,意思是不用密碼登陸

#重啟mysql服務
[root@taoyuan ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

#登陸mysql 修改一個表
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> select * from user;

#查看user表
mysql> select password from user;
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *758ABA8398EF87C993D2C4420DACD8946907C873 |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
6 rows in set (0.00 sec)

#修改密碼
mysql> update user set password=password(‘Aa123456‘) where user=‘root‘;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

#quit ,把/etc/my.cnf 配置文件修改回去 ,重啟mysql服務
[root@taoyuan ~]# vi /etc/my.cnf
[root@taoyuan ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@taoyuan ~]# mysql -uroot -p‘Aa123456‘
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.

註: 完成該操作之後就可以任意登錄mysql了(無需密碼),所以此時mysql安全性很差,平時配置文件中一定不要添加該參數!!

13.2 連接mysql

  • 本機直接登陸
[root@taoyuan ~]# mysql -uroot -pAa123456
  • 通過端口連接(TCP/IP)
[root@taoyuan ~]# mysql -uroot -pAa123456 -h127.0.0.1 -P3306
# -P 指定端口
  • 使用sock的連接
[root@taoyuan ~]# mysql -uroot -pAa123456 -S/tmp/mysql.sock
#適用於本機連接
  • 連接mysql操作一個命令
[root@taoyuan ~]# mysql -uroot -pAa123456 -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常用命令

mysql的設置更改root密碼、連接、常用命令