MYSQL設置更改root密碼
- export PATH=$PATH:/usr/local/mysql/bin/ 把mysql加入環境變量可以直接使用mysql命令,永久生效要把這條命令放到/etc/profile,並source /etc/profile
- 設置密碼
[root@aminglinux-02 ~]# mysqladmin -uroot password 's5381561'
Warning: Using a password on the command line interface can be insecure.
[root@aminglinux-02 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 更改密碼
[root@aminglinux-02 ~]# mysqladmin -uroot -p's5381561' password '123456'
Warning: Using a password on the command line interface can be insecure.
[root@aminglinux-02 ~]# mysql -uroot -p'123456'
mysql>
- 忘記密碼時,進行密碼更改
- 改配置文件
[root@aminglinux-02 ~]# vim /etc/my.cnf
[mysqld]
skip-grant #忽略授權
[root@aminglinux-02 ~]# vim /etc/my.cnf
[root@aminglinux-02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@aminglinux-02 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
- 更改密碼
[root@aminglinux-02 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> use mysql;
#切換mysql庫
Database changed
mysql> select * from user\G;
#查看用戶的表信息,該表中存放的是用戶相關信息(密碼、授權…)
#G選項的作用是使輸出信息有序顯示,不加該選項,顯示內容會很亂
mysql> select password from user;
#查看用戶密碼,顯示結果Wie加密字符串!
mysql> update user set password=password('1234567') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4 Changed: 4 Warnings: 0
#將密碼更改爲‘1234567’
mysql> quit
Bye
- 恢復配置文件並重啓
[root@aminglinux-02 ~]# vim /etc/my.cnf
[root@aminglinux-02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@aminglinux-02 ~]# mysql -uroot -p1234567
Warning: Using a password on the command line interface can be insecure.
連接mysql
- 遠程連接
[root@aminglinux-02 ~]# mysql -uroot -p1234567 -h127.0.0.1 -P3306
Warning: Using a password on the command line interface can be insecure.
- 本地連接
[root@aminglinux-02 ~]# mysql -uroot -p1234567 -S/tmp/mysql.sock
Warning: Using a password on the command line interface can be insecure.
- 顯示所有數據庫,該方法使用於shell腳本中
[root@aminglinux-02 ~]# mysql -uroot -p'1234567' -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 db_name; | 切換庫
- show tables; | 查看庫中的表
- desc tb_name | 查看錶裏的字段
- show create table tb_name\G; | 查看建表語句
- select user(); | 查看當前用戶
- select database(); | 查看當前使用的數據庫
- select * from user\G; | 查看所有用戶
- 編輯庫
- create database db_name; | 創建庫
- use db_name;create table tb_name | 在某庫下創建表
- select version(); | 查看當前數據庫版本
- show status; | 查看數據庫狀態
- show variables; | 查看所有參數
- show variables like 'max_connet%' | 查看某參數,%爲通配符
- set global max_connect_errors=100; | 修改某參數 可以在my.cnf裏永久修改
- show processlist; | 查看mysql進程隊列
- show full processlist | 查看隊列詳細信息
- drop database db_name | 刪除庫
- 代碼
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)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#括號中是定義字段及字段格式,使用反引號引起來
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)
擴展