亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    MySQL常用操作

    一、設(shè)置更改root密碼

    第一次進(jìn)入數(shù)據(jù)庫不需要密碼:

    [root@localhost ~]# /usr/local/mysql/bin/mysql -uroot //-u 指定要登錄的用戶,后面有無空格都行;root為mysql自帶的管理員賬號,默認(rèn)沒有密碼

    Welcome to the MySQL monitor. Commands end with ; or g.

    Your MySQL connection id is 2

    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 //退出時直接輸入quit或exit即可

    Bye

    上面命令使用了絕對路徑,不是很方便,為了更方便,可以修改/etc/profile把/usr/local/mysql/bin加入到環(huán)境變量中:

    [root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

    [root@localhost ~]# source /etc/profile

    [root@localhost ~]# mysql -uroot

    Welcome to the MySQL monitor. Commands end with ; or g.

    Your MySQL connection id is 4

    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>

    這樣就可以不用每次使用絕對路徑了。

    給root用戶設(shè)定密碼:

    [root@localhost ~]# mysqladmin -uroot password '123456' //設(shè)定密碼為123456

    Warning: Using a password on the command line interface can be insecure. //這行為警告信息,意思是在命令行下面暴露了密碼,不安全

    [root@localhost ~]# mysql -uroot

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) //再次登錄,提示錯誤,因?yàn)闆]有密碼

    重新輸入密碼登錄:

    [root@localhost ~]# mysql -uroot -p //-p 后面不可以有空格,可以直接跟密碼,也可以不跟,不跟密碼就是以交互形式輸入密碼

    Enter password:

    Welcome to the MySQL monitor. Commands end with ; or g.

    Your MySQL connection id is 8

    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>

    更改root用戶密碼:

    mysql> SET PASSWORD FOR 'root'@localhost =PASSWORD('1234567'); //之前密碼為123456,現(xiàn)在改為1234567

    Query OK, 0 rows affected (0.00 sec)

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

    Enter password:

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) //輸入123456,提示錯誤

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

    Enter password:

    Welcome to the MySQL monitor. Commands end with ; or g.

    Your MySQL connection id is 4

    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. //輸入1234567,成功登陸

    忘記root用戶密碼時修改密碼:

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

    skip-grant //在[mysqld]下面增加這一行

    [root@localhost ~]# /etc/init.d/mysqld restart //重啟服務(wù)

    Shutting down MySQL.. SUCCESS!

    Starting MySQL. SUCCESS!

    [root@localhost ~]# mysql -uroot //進(jìn)入mysql

    mysql> use mysql; //進(jìn)入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 ('1234567') where user='root'; //修改root密碼

    Query OK, 4 rows affected (0.00 sec)

    Rows matched: 4 Changed: 4 Warnings: 0

    [root@localhost ~]# vim /etc/my.cnf //去掉skip-grant這行

    [root@localhost ~]# /etc/init.d/mysqld restart //重啟服務(wù)

    [root@localhost ~]# mysql -uroot -p //使用新密碼登陸

    二、連接MySQL

    上面我們使用mysql -uroot -p命令來連接數(shù)據(jù)庫,但是連接的只是本地數(shù)據(jù)庫的localhost。而很多時候,我們需要連接網(wǎng)絡(luò)中某一主機(jī)上的mysql。

    [root@localhost ~]# mysql -uroot -p -h192.168.33.128 -P3306 //-h 指定遠(yuǎn)程主機(jī)的IP,-P(大寫)用來指定遠(yuǎn)程主機(jī)mysql的綁定端口

    Enter password:

    三、MySQL常用命令

    在日常工作中,難免會遇到一些與mysql相關(guān)的操作,如建庫、建表、查詢mysql狀態(tài)等,我們需要掌握關(guān)于這些常用的命令。

    查詢當(dāng)前庫

    mysql> show databases; //注意每條命令的最后面都需要跟一個分號作為結(jié)束符號

    +——————–+

    | Database |

    +——————–+

    | information_schema |

    | mysql |

    | performance_schema |

    | test |

    +——————–+

    4 rows in set (0.01 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 //提示會把當(dāng)前庫里的所有表的字段全部讀一段,可以在啟動mysql時加上-A關(guān)閉這個提示,不關(guān)閉也無影響

    Database changed

    然后再把表列出來:

    mysql> show tables;

    +—————————+

    | Tables_in_mysql |

    +—————————+

    | columns_priv |

    | db |

    | event |

    | func |

    | general_log |

    | help_category |

    | help_keyword |

    | help_relation |

    | help_topic |

    | innodb_index_stats |

    | innodb_table_stats |

    | ndb_binlog_index |

    | plugin |

    | proc |

    | procs_priv |

    | proxies_priv |

    | servers |

    | slave_master_info |

    | slave_relay_log_info |

    | slave_worker_info |

    | slow_log |

    | tables_priv |

    | time_zone |

    | time_zone_leap_second |

    | time_zone_name |

    | time_zone_transition |

    | time_zone_transition_type |

    | user |

    +—————————+

    28 rows in set (0.00 sec)

    查看某個表的全部字段

    mysql> desc db; //查看db表的全部字段

    +———————–+—————+——+—–+———+——-+

    | Field | Type | Null | Key | Default | Extra |

    +———————–+—————+——+—–+———+——-+

    | Host | char(60) | NO | PRI | | |

    | Db | char(64) | NO | PRI | | |

    | User | char(16) | NO | PRI | | |

    | Select_priv | enum('N','Y') | NO | | N | |

    | Insert_priv | enum('N','Y') | NO | | N | |

    | Update_priv | enum('N','Y') | NO | | N | |

    | Delete_priv | enum('N','Y') | NO | | N | |

    | Create_priv | enum('N','Y') | NO | | N | |

    | Drop_priv | enum('N','Y') | NO | | N | |

    | Grant_priv | enum('N','Y') | NO | | N | |

    | References_priv | enum('N','Y') | NO | | N | |

    | Index_priv | enum('N','Y') | NO | | N | |

    | Alter_priv | enum('N','Y') | NO | | N | |

    | Create_tmp_table_priv | enum('N','Y') | NO | | N | |

    | Lock_tables_priv | enum('N','Y') | NO | | N | |

    | Create_view_priv | enum('N','Y') | NO | | N | |

    | Show_view_priv | enum('N','Y') | NO | | N | |

    | Create_routine_priv | enum('N','Y') | NO | | N | |

    | Alter_routine_priv | enum('N','Y') | NO | | N | |

    | Execute_priv | enum('N','Y') | NO | | N | |

    | Event_priv | enum('N','Y') | NO | | N | |

    | Trigger_priv | enum('N','Y') | NO | | N | |

    +———————–+—————+——+—–+———+——-+

    22 rows in set (0.00 sec)

    另外,還可以使用這條命令:

    mysql> show create table dbG //這個命令顯示信息更詳細(xì),且會把建表語句全部列出來; G讓列出來的結(jié)果豎排顯示,這樣看起來更清晰,用了G就不用加分號了。

    *************************** 1. row ***************************

    Table: db

    Create Table: CREATE TABLE `db` (

    `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',

    `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',

    `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',

    `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

    PRIMARY KEY (`Host`,`Db`,`User`),

    KEY `User` (`User`)

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Database privileges'

    1 row in set (0.00 sec)

    查看當(dāng)前是哪個用戶

    mysql> select user(); //查看當(dāng)前用戶

    +—————-+

    | user() |

    +—————-+

    | root@localhost |

    +—————-+

    1 row in set (0.00 sec)

    查看當(dāng)前所使用的數(shù)據(jù)庫

    mysql> select database(); //查看當(dāng)前數(shù)據(jù)庫

    +————+

    | database() |

    +————+

    | mysql |

    +————+

    1 row in set (0.00 sec)

    創(chuàng)建一個新庫

    mysql> create database db1; //新建一個庫db1

    Query OK, 1 row affected (0.00 sec)

    創(chuàng)建一個新表

    mysql> use db1 //切換到庫db1

    Database changed

    mysql> create table t1 (`id` int(4),`name` char(40)); //新建表t1,并且寫入數(shù)據(jù),字段名id和name用反引號括起來

    Query OK, 0 rows affected (0.01 sec)

    查看當(dāng)前數(shù)據(jù)庫的版本

    mysql> select version(); //查看當(dāng)前mysql版本

    +———–+

    | version() |

    +———–+

    | 5.6.36 |

    +———–+

    1 row in set (0.00 sec)

    查看MySql的當(dāng)前狀態(tài)

    mysql> show status; //查看當(dāng)前mysql狀態(tài)

    +———————————————–+————-+

    | Variable_name | Value |

    +———————————————–+————-+

    | Aborted_clients | 0 |

    | Aborted_connects | 2 |

    | Binlog_cache_disk_use | 0 |

    | Binlog_cache_use | 0 |

    | Binlog_stmt_cache_disk_use | 0 |

    | Binlog_stmt_cache_use | 0 |

    | Bytes_received | 1124 |

    | Bytes_sent | 25602 |

    | Com_admin_commands | 0 |

    | Com_assign_to_keycache | 0 |

    查看MySql的參數(shù)

    mysql> show variables; //查看mysql各參數(shù)

    | innodb_stats_sample_pages | 8 |

    | innodb_stats_transient_sample_pages | 8 |

    | innodb_status_output | OFF |

    | innodb_status_output_locks | OFF |

    | innodb_strict_mode | OFF |

    | innodb_support_xa | ON |

    | innodb_sync_array_size | 1 |

    | innodb_sync_spin_loops | 30 |

    | innodb_table_locks | ON |

    | innodb_thread_concurrency | 0 |

    | innodb_thread_sleep_delay | 10000 |

    | innodb_tmpdir |

    修改MySql的參數(shù):

    上面列出的很多參數(shù)都是可以在/etc/my.cnf中定義的。

    以參數(shù) max_connect_errors為例,修改它:

    mysql> show variables like 'max_connect%'; //mysql中,符號%類似于shell下的*,表示通配

    +——————–+——-+

    | Variable_name | Value |

    +——————–+——-+

    | max_connect_errors | 100 |

    | max_connections | 151 |

    +——————–+——-+

    2 rows in set (0.00 sec)

    mysql> set global max_connect_errors = 1000; //修改max_connect_errors的值為1000,set global可以臨時修改一些參數(shù),重啟mysql服務(wù)失效,修改配置文件my.cnf才能永久生效

    Query OK, 0 rows affected (0.00 sec)

    mysql> show variables like 'max_connect%';

    +——————–+——-+

    | Variable_name | Value |

    +——————–+——-+

    | max_connect_errors | 1000 |

    | max_connections | 151 |

    +——————–+——-+ //max_connect_errors的值為1000

    2 rows in set (0.00 sec)

    查看當(dāng)前MySql服務(wù)器的隊(duì)列

    查看服務(wù)器隊(duì)列在日常工作中用的最多,使用它可以查看mysql當(dāng)前在干什么,也可以發(fā)現(xiàn)是否有鎖表。

    查看服務(wù)器隊(duì)列:

    mysql> show processlist; //查看服務(wù)器隊(duì)列

    +—-+——+———–+——+———+——+——-+——————+

    | Id | User | Host | db | Command | Time | State | Info |

    +—-+——+———–+——+———+——+——-+——————+

    | 11 | root | localhost | db1 | Sleep | 9373 | | NULL |

    | 12 | root | localhost | NULL | Query | 0 | init | show processlist |

    +—-+——+———–+——+———+——+——-+——————+

    2 rows in set (0.00 sec)

    四、MySQL用戶管理

    創(chuàng)建一個普通用戶并授權(quán)

    mysql> grant all on *.* to user1 identified by '123456';

    Query OK, 0 rows affected (0.00 sec)

    1. all表示所有的權(quán)限(如讀、寫、查詢、刪除等操作);

    2. . 旁邊有兩個匹配條件,前面的*表示所有的數(shù)據(jù)庫,后面的*表示所有的表;

    3. identified by后面跟密碼,用單引號括起來,這里user1指的是localhost上的user1

    給網(wǎng)絡(luò)上其他機(jī)器上的某用戶授權(quán):

    mysql> grant all on db1.* to 'user2'@'192.168.33.128' identified by '111222';

    Query OK, 0 rows affected (0.00 sec)

    用戶和主機(jī)的IP都用單引號括起來,兩者之間有@符號,IP可以用%代替,表示所有主機(jī)

    五、常用SQL語句

    查詢語句select

    第一種形式:

    mysql> select count(*) from mysql.user;

    +———-+

    | count(*) |

    +———-+

    | 8 |

    +———-+

    1 row in set (0.00 sec)

    //mysql.user 表示mysql庫的user表,count(*)表示表中共有多少行

    第二種形式:

    mysql> select * from mysql.db;

    +—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+

    | Host | Db | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |

    +—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+

    | % | test | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |

    | % | test_% | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |

    | 192.168.33.128 | db1 | user2 | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |

    +—————-+———+——-+————-+————-+————-+————-+————-+———–+————+—————–+————+————+———————–+——————+——————+—————-+———————+——————–+————–+————+————–+

    3 rows in set (0.00 sec)

    //上面表示查詢mysql庫的db表中的所有數(shù)據(jù),謹(jǐn)慎使用該命令,因?yàn)樾枰馁M(fèi)很大的cpu資源

    查詢單個字段或多個字段:

    mysql> select db from mysql.db //查詢單個字段

    mysql> select db,user from mysql.db //查詢多個字段

    使用%查詢:

    mysql> select * from mysql.db where host like '192.168.%' //查詢IP為192.168網(wǎng)段的mysql庫的db表中的所有數(shù)據(jù)

    插入一行insert

    插入操作在mysql中也很常見。

    – 插入:

    mysql> insert into db1.t1 values (1,'abc'); //向db1庫的t1表中插入1,abc

    Query OK, 1 row affected (0.01 sec)

    mysql> select * from db1.t1;

    +——+——+

    | id | name |

    +——+——+

    | 1 | abc |

    +——+——+

    1 row in set (0.00 sec)

    更改表的某一行update

    mysql表里存放的數(shù)據(jù)支持更改某個字段。

    – 更改:

    mysql> update db1.t1 set name='aaa' where id=1; //更改id=1的name為aaa

    Query OK, 1 row affected (0.00 sec)

    Rows matched: 1 Changed: 1 Warnings: 0

    mysql> select * from db1.t1;

    +——+——+

    | id | name |

    +——+——+

    | 1 | aaa |

    +——+——+

    1 row in set (0.00 sec)

    清空某個表的數(shù)據(jù)truncate

    有時候我們不想刪除表,只想清空數(shù)據(jù)。

    – 清空:

    mysql> truncate table db1.t1; //清空db1庫的t1表

    Query OK, 0 rows affected (0.01 sec)

    mysql> select * from db1.t1;

    Empty set (0.00 sec)

    刪除表drop table

    如果某個表不需要了,那就直接刪除。

    – 刪除表:

    mysql> drop table db1.t1; //刪除db1庫的t1表

    Query OK, 0 rows affected (0.01 sec)

    六、MySql數(shù)據(jù)庫的備份與恢復(fù)

    MySql備份mysqldump

    [root@localhost ~]# mysqldump -uroot -p'123456' mysql > /tmp/mysql.sql

    Warning: Using a password on the command line interface can be insecure.

    //-u 和 -p 的作用和前面一樣,后面的mysql指的是庫名,然后重定向到一個文檔里

    MySql恢復(fù)

    [root@localhost ~]# mysql -uroot -p'123456' mysql < /tmp/mysql.sql //恢復(fù)時使用的是mysql命令而不是mysqldump。

    Warning: Using a password on the command line interface can be insecure.

    擴(kuò)展:

    mysql5.7 root密碼更改

    myisam 和innodb引擎對比

    mysql 配置詳解

    mysql調(diào)優(yōu)

    同學(xué)分享的親身mysql調(diào)優(yōu)經(jīng)歷

    SQL語句教程

    什么是事務(wù)?事務(wù)的特性有哪些

    根據(jù)binlog恢復(fù)指定時間段的數(shù)據(jù)

    mysql字符集調(diào)整

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號