設置更改root密碼 鏈接mysql 及mysql經常使用命令

6月19日任務

13.1 設置更改root密碼
13.2 鏈接mysql
13.3 mysql經常使用命令
擴展 
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.htmlhtml

設置更改root密碼目錄概要

  • /usr/local/mysql/bin/mysql -uroot
  • 更改環境變量PATH,增長mysql絕對路徑
  • mysqladmin -uroot password '123456'
  • mysql -uroot -p123456
  • 密碼重置
  • vi /etc/my.cnf//增長skip-grant
  • 重啓mysql服務 /etc/init.d/mysqld restart
  • mysql -uroot
  • use mysql;
  • update user set password=password('aminglinux') where user='root';

設置更改root密碼

  • root用戶是mysql的超級管理員用戶,和linux系統的root用戶相似,不過和Linux的不同
  • 默認mysql的 root 用戶密碼是空的,直接就能夠鏈接上去,不須要輸入密碼,可是不安全,因此就須要設置一個密碼
  • 爲了方便使用mysql服務,將mysql目錄加入到環境變量裏
  1. 打開系統,查看mysql是否啓動 ps aux|grep mysql
  2. 如果沒有啓動mysql的話,將mysql啓動起來 service mysqld start 
  3. 在啓動mysql後,使用mysql -uroot命令,可是mysql命令會提示不存在,由於安裝的mysql是在/usr/local/mysql/bin/mysql,而這個目錄並不在環境變量PATH裏面,因此它會報錯 ,若想要這個命令直接運行,須要把PATH作一個更改
[root@yong-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@yong-01 ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@yong-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 
[root@yong-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
  • 這時再來使用mysql -uroot命令就會發現可使用了 退出mysql輸入 quit;或者exit; 便可
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> quit
Bye
  1. 若想要變量永久生效,還須要將export PATH=$PATH:/usr/local/mysql/bin/  放入到 /etc/profile 最後面
  2. 假設如果沒有運行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能運行mysql,由於變量尚未生效,想要這個變量生效,在配置文件中加入命令後,還須要執行source /etc/profile 命令,從新加載
  3. 通常是使用mysql -uroot -p命令  -p,表示指定密碼
  4. 密碼爲空的時候,直接回車就可進入到mysql,並能夠在其中操做一些mysql的一些行爲
  5. 退出mysql,輸入 quit 便可
  6. 設置mysql密碼,命令爲mysqladmin -uroot passwd '123456' 在 ' ' 爲密碼
[root@yong-01 ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
  • 在設置密碼的時候,會看到有輸出信息,但這不是報錯信息,這是告訴你 你如今密碼在當前命令行顯示出來了,這樣不太安全
  • 這時在想直接登陸mysql,就會提示須要輸入密碼了
[root@yong-01 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  • 在使用-p,並輸入密碼就能夠正常的進入到mysql命令行了
[root@yong-01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

知道mysql的root密碼,去更改密碼

  1. 如果這時知道mysql密碼,去修改mysql密碼,看到輸出的提示信息不用去理會
  2. 格式
    • mysqladmin -uroot -p'123456' password '654321'
[root@yong-01 ~]# mysqladmin -uroot -p'123456' password '654321'
Warning: Using a password on the command line interface can be insecure.
  • 指定新密碼去登陸,固然也能夠不明文指定密碼,知道-p回車,輸入密碼登陸也行
[root@yong-01 ~]# mysql -uroot -p'654321'
Warning: Using a password on the command line interface can be insecure.
  • 在明文指定密碼的時候,密碼能夠加單引號,也能夠不加單引號,建議加上單引號,防止密碼有特殊符號的存在——>(如果不加單引號,而密碼中又有特殊符號,就有可能會不識別)

不知道mysql的root密碼,去更改密碼

  1. 在不知道mysql的root用戶密碼的時候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
  2. skip-grant ,表示忽略受權,也就是說操做mysql的時候不須要用戶名和密碼了,能直接登陸
  3. 在更改配置文件後,重啓mysql服務 service mysqld restart
  4. 這時候在輸入mysql -uroot ,會發現直接進入mysql,而不須要密碼了
[root@yong-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
  • 在登陸進mysql後,還須要更改一個表,由於用戶名和密碼是存在於一個mysql庫裏面的,使用 use mysql; 切換庫,在切換到mysql庫裏面,而後去更改一個存用戶名密碼的user表
  • use 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>
  1. 查看user表,輸入select * from user; 命令,會看到輸出亂七八糟的亂碼,裏面存放的就是用戶名和密碼,還有權限和受權等信息
  2. 查看password表,會看到密碼都是加密的
mysql> select password from user where user='root';
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
4 rows in set (0.05 sec)
  • 更改user表,使用update user set password=password('111111') where user='root'; 命令
  • update user set password=password('111111') where user='root';

 

密碼字段       函數 //用於加密密碼    高亮部分:爲條件語句
mysql> update user set password=password('111111') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
  1. 提示說4行修改完畢,即便有些行是空的
  2. 這樣密碼就更改爲功了,輸入quit退出數據庫便可
  3. 再去 /etc/my.cnf 配置文件中刪除免受權配置,即刪除skip-grant——>如果不刪除,那麼以後全部的用戶都不須要輸入密碼,就能夠登陸進去,這樣安全性過低
  4. 重啓mysql服務 service mysqld restart
  5. 重啓完以後,再用新的密碼測試下,會看到新的密碼能夠登陸 mysql -uroot -p'111111'
  6. 這樣就是成功更改mysql密碼

鏈接mysql

  • 本地鏈接——>即便沒有指定,但默認使用sock鏈接,使用/tmp/mysql.sock鏈接
    • mysql -uroot -p123456 //輸入用戶名和密碼鏈接本機
  • 使用ip端口鏈接遠程機器
    • mysql -uroot -p'111111' -h[遠程mysql主機IP] -P[端口]
[root@yong-01 ~]# mysql -uroot -p'111111' -h127.0.0.1 -P3306
  • 使用sock方式鏈接(只適合在本機使用)如爲指定IP就用sock訪問
    •  mysql -uroot -p'111111' -S/tmp/mysql.sock
[root@yong-01 ~]# mysql -uroot -p'111111' -S/tmp/mysql.sock
  • 鏈接mysql以後執行命令(經常使用於shell腳本)
  • mysql -uroot -p123456 -e 「show databases」
  • show databases //列出全部數據庫
[root@yong-01 ~]# mysql -uroot -p'111111' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

mysql經常使用命令

  • 查詢庫 show databases;
  • 切換庫 use mysql;
  • 查看庫裏的表 show tables;
  • 查看錶裏的字段 desc tb_name;
  • 查看建表語句 show create table tb_name\G;
  • 查看當前用戶 select user();
  • 查看當前使用的數據庫 select database();
  • 建立庫 create database db1;
  • 建立表 use db1; create table t1(id int(4), name char(40));
  • 查看當前數據庫版本 select version();
  • 查看數據庫狀態 show status;
  • 查看各參數 show variables; show variables like 'max_connect%';
  • 修改參數 set global max_connect_errors=1000;
  • 查看隊列 show processlist; show full processlist;

建立庫

  • 建立庫 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
  • 查看db1庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
  • 切換到db1庫下面去
mysql> use db1;
Database changed
  • 建立表 create table t1(id int(4), name char(40));——>定義 id 和 name ,並用反引號括起來
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.03 sec)
  • 查看建立的表t1,默認使用的是InnoDB 引擎
mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified
  1. 在數據庫裏面取消命令,即在命令的最前面加一個 # 號就不會生效了
  2. 刪除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)
  • 建立t1表
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.06 sec)
  • 查看錶,會看到變成了utf8
mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified
  • 查看當前數據庫版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)
  • 查看數據庫狀態 show status; 它會把經常使用的數據都列出來
  • 查看各參數 show variables; show variables like 'max_connect%';      //  mysql下 % 爲通配符
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)
  • 修改參數 set global max_connect_errors=1000; ——>僅在內存中生效
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)
  • 若想重啓依然生效,那須要修改配置文件/etc/my.cnf

查看隊列

  • show processlist; //查看庫的情況,好比,那些用戶在連,作了些什麼操做,是否鎖表
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.00 sec)
  • show full processlist; //查看到的對列,最後一個會很是完成的顯示出來
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql>
相關文章
相關標籤/搜索