Linux下安裝以及使用MySQL資料庫
1、官網下載mysql資料庫:https://dev.mysql.com/downloads/mysql/
2、linux 下可直接下載:wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
3、解壓縮到 /usr/local/mysql 下
4、進入到mysql的根目錄 安裝:bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
5、安裝完成後,方便以後操作,將服務新增到service中,並新增環境變數到PATH中
1)拷貝:# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2)將mysql服務加入chkconfig管理列表 # chkconfig --add mysqld
3)設定終端模式開機啟動 # chkconfig mysqld on
4)修改環境變數# vi /etc/profile 新增
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
5)使變數生效 # source /etc/profile
6、啟動mysql服務 # service mysqld start
7、修改MySQL root 密碼:
1)跳過密碼驗證:# vi /etc/my.cnf
在[mysqld] 下邊新增skip-grant-tables
2)重啟資料庫 # service mysqld restart
2)登陸 # mysql -uroot -p 直接回車即可登陸
3)使用資料庫mysql 更改root密碼為[email protected]
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 authentication_string = password('[email protected]') where user='root';
mysql>flush privileges;
4)退出後將跳過密碼驗證刪掉或者註釋掉 重啟資料庫 然後用密碼進行連線;
8、如果root密碼安全性比較低會報錯提示更改root密碼
mysql>alter user 'root'@'localhost' identified by '[email protected]';
9、資料庫一些操作
1)檢視當前使用者下的資料庫
mysql> show databases;
2)切換資料庫 use 資料庫名 (mysql預設使用"mysql"資料庫中的表user存放使用者資訊)
mysql> use mysql;
Database changed
3)建立資料庫
mysql> create database test;
Query OK, 1 row affected (0.13 sec)
4)刪除資料庫
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
5)匯出資料表
5.1 匯出指定資料庫結構+資料:# mysqldump -u使用者名稱 -p 資料庫名 > 匯出的檔名
eg:# mysqldump -uroot -p mysql > mysql.sql
備註:預設匯出資料庫名稱為“mysql”的庫到使用者當前執行語句的目錄
5.2 匯出指定資料庫的指定表結構+資料:# mysqldump -u使用者名稱 -p 資料庫名 表名> 匯出的檔名
eg:# mysqldump -uroot -p mysql user > mysql_user.sql
5.3 匯出指定資料庫指定表的表結構 :# mysqldump -u使用者名稱 -p -d --add-drop-table 資料庫名 表名> 匯出的檔名
eg:# mysqldump -uroot -p -d --add-drop-table mysql user > mysql_user_create.sql
備註:-d 沒有資料 --add-drop-table 建立表之前加上刪除表語句
6)匯入資料表
mysql>source 檔名
eg:mysql> source mysql_user.sql
備註:要匯入的檔案 可以是絕對路徑,絕對路徑是連線資料庫的時候的路徑
7)查看錶:
mysql> show tables;
+-------------------+
| Tables_in_newTest |
+-------------------+
| user |
+-------------------+
1 row in set (0.00 sec)
8)檢視資料庫中有多少張表 ,查詢information_schema庫中的表tables
eg:查詢所有資料庫中的表的數量
mysql> select count(*), table_schema from information_schema.tables group by table_schema order by 1;
+----------+--------------------+
| count(*) | table_schema |
+----------+--------------------+
| 1 | newTest |
| 1 | test |
| 31 | mysql |
| 61 | information_schema |
| 87 | performance_schema |
| 101 | sys |
+----------+--------------------+
6 rows in set (0.01 sec)
eg:查詢指定庫的表的數量
mysql> select count(*),table_schema from information_schema.tables where table_schema = 'mysql';
+----------+--------------+
| count(*) | table_schema |
+----------+--------------+
| 31 | mysql |
+----------+--------------+
1 row in set (0.00 sec)