MySQL版本 5.6.35mysql
首次直接使用mysql會提示‘該命令不存在’,緣由是尚未將該命令加入環境變量,若是要使用該命令,須要使用其絕對路徑:/usr/local/mysql/bin/mysql,爲了方便,先將其加入系統環境變量:sql
[root@1 ~]# exprt PATH=$PATH:/usr/local/mysql/bin/
至此,mysql命令路徑暫時加入環境變量,系統重啓後該變量會失效,若要永久生效,須要將其加入環境變量配置文件:shell
[root@1 ~]# vim /etc/profile …… export PATH=$PATH:/usr/local/mysql/bin/ 刷新配置文件(不然不生效): [root@1 ~]# source /etc/profile
首次登錄mysql,root用戶沒有密碼,直接登陸:數據庫
[root@1 ~]# mysql -uroot #-u:=user,指定用戶名 Welcome to the MySQL monitor. Commands end with ; or \g. …… mysql> quit #退出
說明: 登陸mysql以後能夠進行與mysql相關的一些操做,可是設置mysql用戶的密碼須要執行如下操做!vim
[root@1 ~]# mysqladmin -uroot password '123456' 再次登陸: [root@1 ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
說明: 設置密碼後直接登陸會報錯(ERROR),須要輸入密碼登陸。安全
[root@1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. mysql>
注: -p=passwd,使用密碼登陸,在此能夠將密碼直接輸入在命令行(跟在-p後面,不加空格:-p'123456'<此處單引號能夠不加,可是當密碼中有特殊符號時必須加,因此在命令行輸入密碼時養成習慣:加單引號>),也能夠不在命令行輸入,只跟-p選項,而後根據提示信息:「Enter password」,輸入密碼進行登陸(此方法不會暴露用戶密碼,安全)。dom
[root@1 ~]# mysqladmin -uroot -p'123456' password '1234567' [root@1 ~]# mysql -uroot -p'1234567' Welcome to the MySQL monitor. mysql>
更改爲功!socket
先編輯mysql配置文件: [root@1 ~]# vim /etc/my.cnf [mysqld] skip-grant #忽略受權! datadir=/data/mysql socket=/tmp/mysql.sock 重啓mysql服務: [root@1 ~]# /etc/init.d/mysqld restart Shutting down MySQL... SUCCESS! Starting MySQL..................... SUCCESS!
說明: 完成該操做以後就能夠任意登陸mysql了(無需密碼),因此此時mysql安全性不好,平時配置文件中必定不要添加該參數!!!ui
[root@1 ~]# 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
密碼更改爲功!this
恢復配置文件: [root@1 ~]# vim /etc/my.cnf [mysqld] datadir=/data/mysql socket=/tmp/mysql.sock 重啓mysql服務: [root@1 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL........... SUCCESS! 登陸: [root@1 ~]# 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。
[root@1 ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306 Welcome to the MySQL monitor. mysql> quit Bye
注: -h:=host,指定IP;-P:=port,指定端口。
[root@1 ~]# mysql -uroot -p123456 -S/tmp/mysql.sock Welcome to the MySQL monitor. mysql> quit Bye
注: -S:=socket,指定socket。此方法只適用於本地鏈接,等同於「mysql -uroot -p123456」。
[root@1 ~]# 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腳本中。
查看庫信息:
•查詢庫 show databases;
• 切換庫 use mysql;
• 查看庫裏的表 show tables;
• 查看錶裏的字段 desc tb_name;
• 查看建表語句 show create table tb_name\G;
• 查看當前用戶 select user();
• 查看當前使用的數據庫 select databsase();
注: 以上命令均須要在mysql下執行;在mysql中每行命令末尾加上分號,表示該行命令執行結束。 tb_name即table name()表名。
[root@1 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)
與MySQL 5.6版本不一樣,在安裝MySQL 5.7過程當中(初始化)會自動生成root用戶密碼(隨機),那麼在安裝完成後如何更改root用戶密碼?步驟以下:
[root@1 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@1 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@1 mysql]# vi /etc/my.cnf [mysqld] skip-grant-tables datadir=/data/mysql socket=/tmp/mysql.sock #增長參數:skip-grant-tables 重啓: [root@1 mysql]# /etc/init.d/mysqld restart 登陸:此時不須要密碼 [root@1 mysql]# /usr/local/mysql/bin/mysql -uroot 更改密碼: mysql> update user set authentication_string=password('12456') where user='root'; mysql>quit [root@1 mysql]# vi /etc/my.cnf [mysqld] datadir=/data/mysql socket=/tmp/mysql.sock 重啓: [root@1 mysql]# /etc/init.d/mysqld restart
Finished!