13.1 設置更改root密碼html
13.2 鏈接mysqlmysql
13.3 mysql經常使用命令linux
擴展 web
mysql5.7 root密碼更改 http://www.apelearn.com/bbs/thread-7289-1-1.htmlsql
myisam 和innodb引擎對比 http://www.pureweber.com/article/myisam-vs-innodb/shell
知乎上的答案 https://www.zhihu.com/question/20596402數據庫
mysql 配置詳解:https://www.jb51.net/article/48082.htmvim
mysql調優: http://www.aminglinux.com/bbs/thread-5758-1-1.html安全
同窗分享的親身mysql調優經歷: http://www.apelearn.com/bbs/thread-11281-1-1.htmlbash
13.1 設置更改root密碼:
這裏的root用戶是MySQL的超級管理用戶,跟linux操做系統的root相似,可是要注意區分,並非一個用戶,固然也能夠建立一個普通用戶去鏈接MySQL。
默認MySQL的root用戶密碼是空,直接連上去不用輸密碼就OK。可是這樣不安全因此要給他設置一個密碼。可是呢設置完了密碼,好長時間沒用忘記了,怎麼去重置,下面就是這一部份內容:
由於以前咱們用的MySQL是在/usr/local下,不能直接用mysql這個命令,因此要改一下他的變量
!!(小插曲,阿鑫在作的時候,提示報錯ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)。百度後,在/etc/my.cnf里加入了
[client]
socket = /Data/mysql/mysql/mysql.sock (啓動腳本的配置文件)
[mysqld]
socket = /Data/mysql/mysql/mysql.sock
就能夠了) mysql.sock的文件或許不能放在/tmp下?
~1./usr/local/mysql/bin/mysql -uroot
~2.更改環境變量PATH,增長mysql絕對路徑
~3.mysqladmin -uroot password 'wangxin789'
~4.mysql -uroot -pwangxin789
~5.密碼重置
知道root密碼的狀況下:mysqladmin -uroot -p 'wangxin789' password 'axin789'
不知道root密碼的狀況下:
~6.vi /etc/my.cnf //[mysqld]增長 skip-grant(忽略受權,可直接登陸)
~7. /etc/init.d/mysqld restart //重啓mysql服務
~8.mysql -uroot //直接登陸
~9.use mysql; //切換庫(使用mysql這個庫來修改密碼)
~10.update user set password=password('wangxin789') where user='root';
~11.//將/etc/my.cnf的skip-grant去掉,並重啓
實例:
[root@axinlinux-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/ #加上 export後,意味着這個PATH變量能夠在當前shell以及子shell中生效了。要讓這個變量永久生效還要把這個變量放在/etc/profile下
[root@axinlinux-01 ~]# vim /etc/profile #複製這條變量,放在最後面就能夠了
[root@axinlinux-01 ~]# source /etc/profile #還要執行source。在當前bash環境下讀取並執行FileName中的命令
[root@axinlinux-01 ~]# mysqladmin -uroot password 'wangxin789' #命令行設置密碼
[root@axinlinux-01 ~]# mysql -uroot -p #命令行登陸
Enter password: 輸入密碼便可
MySQL [(none)]> quit 輸入quit退出
Bye
[root@axinlinux-01 ~]# mysqladmin -uroot -p'wangxin789' password 'axin789' #命令行修改密碼
[root@axinlinux-01 ~]# mysql -uroot -p #再次登陸
Enter password: #輸入密碼便可
[root@axinlinux-01 ~]# vim /etc/my.cnf #增長skip-grant
[root@axinlinux-01 ~]# /etc/init.d/mysqld restart #重啓
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@axinlinux-01 ~]# mysql -uroot #直接進入便可
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use mysql; #use mysql; 切換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 [mysql]> select * from user; #select * from user; 這條命令能夠看一下user表(存放用戶密碼權限等)
以上輸出爲亂碼(加密)
MySQL [mysql]> update user set password=password('wangxin789') where user='root';
#這兩個password的意思是。一個是要設置的密碼,另外一個是password這個函數(咱們修改密碼就是用password這個函數來修改的)
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MySQL [mysql]> quit
Bye
[root@axinlinux-01 ~]# vim /etc/my.cnf #將skip-grant去掉
[root@axinlinux-01 ~]# /etc/init.d/mysqld restart #並重啓
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@axinlinux-01 ~]# mysql -uroot -p #用新的密碼便可登陸
Enter password:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13.2 鏈接mysql:
~1.mysql -uroot -p123456
鏈接本機。輸入用戶名和密碼
~2.mysql -uroot -p123456 -h127.0.0.1 -P3306
鏈接遠程。好比從A服務器鏈接B的MySQL。輸入遠程的IP和端口(大P默認端口3306,能夠改或3307)
~3.mysql -uroot -p123456 -S/tmp/mysql.sock
MySQL也可使用sock來鏈接(!!只適合在本機)。跟~1.鏈接差很少,雖然沒有指定sock可是默認就是這個sock。阿鑫在作的時候sock不在/tmp下
~4.mysql -uroot -p123456 -e 「show databases」
-e列出他的數據庫。命令行多用於腳本之中
實例:
~2.
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -h127.0.0.1 -P3306
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> quit
Bye
~3.
[root@axinlinux-01 ~]# ps aux |grep mysql 看一下sock路徑
root 1165 0.0 0.0 115432 1704 ? S 06:39 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/axinlinux-01.pid
mysql 1377 0.5 24.3 1368532 457084 ? Sl 06:39 0:04 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=axinlinux-01.err --pid-file=/data/mysql/axinlinux-01.pid --socket=/data/mysql/mysql/mysql.sock
root 1847 0.0 0.0 112720 976 pts/0 R+ 06:52 0:00 grep --color=auto mysql
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -S/data/mysql/mysql/mysql.sock
用大S(-S)來指定sock。後面跟ps後的路徑便可
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -e "show databases"
下面四個就是他的庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13.3 mysql經常使用命令:
~1.查詢庫 show databases; 均以;做爲命令結尾
~2.切換庫 use mysql;
~3.查看庫裏的表 show tables;
~4.查看錶裏的字段 desc tb_name;
例如desc user查看一user表裏有哪些字段(通常交由開發去作)
~5.查看建表語句 show create table tb_name\G;
\G會顯示的更加規整,加\G可不用加;做爲命令結尾
~6.查看當前用戶 select user();
~7.查看當前使用的數據庫 select database();
注意此時這個database後面沒有s
~8.建立庫 create database db1;
~9.建立表 use db1; create table t1(`id` int(4),`name` char(40));
在db1庫裏,建立表叫t1。定義字段。第一個字段叫id,int(4)表示定義它的格式最長是4,第二個字段叫name,char(40)定義字符串最長是40。使用反引號
也可指定他的CHARSET=latin1默認是latin1.可設置成utf8
~10.查看當前數據庫版本 select version();
~11.查看數據庫狀態 show status; 未來生產環境中會常常用到
~12.查看各參數 show variables; show variables like 'max_connect%';
%表示統配(只記得前面的後面記不清了,可使用)
~13.修改參數 set global max_connect_errors=1000;
固然也能夠在/etc/my.cnf配置文件裏修改。也能夠在命令行裏面,在內存中生效
想要開機生效就要在my.cnf裏的[mysqld]里加入max_connect_errors=1000
~14.查看隊列 show processlist; show full processlist;
!!查看mysql到底在幹什麼。哪些用戶在連他,連他的時候在執行什麼操做,有沒有鎖表。會常常用到
相似於linux裏用ps或top查看有哪些操做
full表示完整的。於前面的相比,full更加的完整
實例:
~1.
MySQL [(none)]> show databases; #以分號做爲結束語
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
~2.
MySQL [(none)]> 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
~3
MySQL [mysql]> show tables; #查看錶
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func
......
~4.
MySQL [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
......
~5.
MySQL [mysql]> show create table user\G #更加詳細,排列更加規矩。可不加;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
......
~6.
MySQL [mysql]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
~7.
MySQL [mysql]> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
~8.
MySQL [mysql]> create database db1;
Query OK, 1 row affected (0.01 sec)
MySQL [mysql]> show databases; #看一下庫。就有了咱們建立的這個db1
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1
~9.
MySQL [mysql]> use db1 先進入到db1這個新建測試庫裏
Database changed
MySQL [db1]> create table t1(`id` int(4),`name` char(40)); 新建這個表
Query OK, 0 rows affected (0.08 sec)
MySQL [db1]> 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)
MySQL [db1]> drop table t1; #先刪除咱們以前創的表t1
Query OK, 0 rows affected (0.02 sec)
MySQL [db1]> create table t1(`id` int(4),`name` char(40)) CHARSET=utf8; #而後咱們在從新建立t1的時候指定他的CHARSET=utf8
Query OK, 0 rows affected (0.04 sec)
MySQL [db1]> show create table t1\G #再次查看剛創的這個表。CHARSET就=utf8
*************************** 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)
~10.
MySQL [db1]> select version(); #查看版本
+-----------+
| version() |
+-----------+
| 5.6.39 |
+-----------+
1 row in set (0.00 sec)
~12.
MySQL [(none)]> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.01 sec)
~14
MySQL [(none)]> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 2 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
MySQL [(none)]> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 2 | root | localhost | NULL | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+