1. 程式人生 > >2018-3-22 13周4次課 MySQL常用操作(上)

2018-3-22 13周4次課 MySQL常用操作(上)

MySQL 13周 4次課

13.1 設置更改root密碼


默認MySQL密碼為空


[root@localhost ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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

(如果想要配置永久生效,還需將把export加入/etc/profile)

技術分享圖片

如果只是更改了profile文件,並沒有執行export PATH,那麽source /etc/profile可使環境變量生效


·設置mysql密碼:

[root@localhost ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.

(最好命令行裏不要包含密碼)


·登錄mysql:

[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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


·如果不知道mysql的密碼,進行密碼重置:


1,編輯 /etc/my.cnf ,增加skip-grant:

[root@localhost ~]# vi /etc/my.cnf

技術分享圖片

[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!


2,進入mysql,use mysql,update user set password=password('密碼') where user='root';

[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> 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> update user set password=password('123456') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 4  Changed: 3  Warnings: 0

mysql> quit
Bye


3,編輯 /etc/my.cnf ,去掉skip-grant:

[root@localhost ~]# vi /etc/my.cnf

(去掉之前增加的skip-grant)


4,重啟mysql:

[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
root@localhost ~]# mysql -uroot -p                ##可以重新登錄mysql了
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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





13.2 連接mysql


·連接本機MySQL:

[root@localhost ~]# mysql -uroot -p123456


·連接其他機器MySQL:

[root@localhost ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306

(-h 指定ip,-P 指定端口)


·使用socket連接MySQL:

[root@localhost ~]# mysql -uroot -p123456 -S/tmp/mysql.sock

(-S 指定socket位置,只適合在本機)


·連接MySQL後進行一些操作:

[root@localhost ~]# mysql -uroot -p123456 -e "show databases"





13.3 mysql常用命令


·查詢庫 show database;

[root@localhost ~]# mysql -uroot -p123456
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 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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;

技術分享圖片


·切換庫 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


·查看庫裏的表 show tables;

mysql> show tables;

技術分享圖片


·查看表裏的字段 desc tb_name;

mysql> desc user;

技術分享圖片


·查看建表語句 show create table tb_name\G;

技術分享圖片


·查看當前用戶 select user();

技術分享圖片


·查看當前使用的數據庫 select databsase();

技術分享圖片


·創建庫 create database db1;

技術分享圖片


·創建表 use db1; create table t1(`id` int(4), `name` char(40));

技術分享圖片

如果想要定義其中的設置,可以

mysql> create table b1(`id`int(4),`name`char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.01 sec)


·查看當前數據庫版本 select version();

技術分享圖片


·查看數據庫狀態 show status;

技術分享圖片

(數據不全部展示)


·查看各參數 show variables; show variables like 'max_connect%';

show variables;

(參數太多)

技術分享圖片


·修改參數 set global max_connect_errors=1000;

技術分享圖片

如果想要永久生效,需要退出到shell,vim /etc/my.cnf,定義max_connect_errors=1000;


·查看隊列 show processlist; show full processlist;

技術分享圖片

技術分享圖片


如有錯誤,歡迎指正,互相學習,共同進步!!!

2018-3-22 13周4次課 MySQL常用操作(上)