為什麼選擇MySQL資料庫? 毫無疑問,絕大多數的使用linux操作系統的大中小型互聯網網站都在使用MySQL作為其後端的資料庫存儲,從大型的BAT門戶,到電商平臺,分類門戶等無一例都使用MySQL資料庫。 My Sql 資料庫優點: 1、性能卓越,服務穩定,很少出現異常宕機 2、開放源代碼且無版權 ...
為什麼選擇MySQL資料庫?
毫無疑問,絕大多數的使用linux操作系統的大中小型互聯網網站都在使用MySQL作為其後端的資料庫存儲,從大型的BAT門戶,到電商平臺,分類門戶等無一例都使用MySQL資料庫。
My Sql 資料庫優點:
1、性能卓越,服務穩定,很少出現異常宕機
2、開放源代碼且無版權約束,自主性及使用成本低
3、歷史悠久,社區及用戶非常活躍,遇到問題,可以尋求幫助
4、軟體體積小,安排使用簡單,並且易於維護,安裝及維護成本低
5、品牌口碑效應,使得企業無需考慮直接用之,LAMP,LEMP流行架構
6、支持多操作系統,提供多種API介面,支持多種開發語言,特別對流行的PHP語言有很好支持
linux軟體的安裝方式:
1、 yum/rpm:簡單 快,無法定製。
2、 編譯安裝:比較複雜,速度慢,可定製。
./configure;make;make install gmake;gmake insall
3、 二進位包*****
直接解壓就能用(類似於綠色軟體,無需安裝) 簡單,快,不好定製。
下麵我們選擇二進位包方法:
1、創建mysql用戶
[root@lamp01 tools]# id mysql
id: mysql:無此用戶
[root@lamp01 tools]# groupadd mysql
[root@lamp01 tools]# useradd -s /sbin/nologin -g mysql -M mysql
[root@lamp01 tools]# id mysql
uid=503(mysql) gid=503(mysql) 組=503(mysql)
[root@lamp01 tools]#
2、下載或上傳mysql二進位軟體包並解壓
[root@lamp01 tools]# tar xf ./mysql-5.5.32-linux2.6-x86_64.tar.gz
[root@lamp01 tools]# ll
總用量 182352
drwxr-xr-x. 13 root root 4096 1月 10 21:54 mysql-5.5.32-linux2.6-x86_64
-rw-r--r--. 1 root root 186722932 1月 10 21:51 mysql-5.5.32-linux2.6-x86_64.tar.gz
3、將解壓的文件移動到安裝目錄下,並做軟連接(隱藏版本號安全)
[root@lamp01 tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
[root@lamp01 tools]# ln -s /application/mysql-5.5.32/ /application/mysql
[root@lamp01 tools]# ll /application/總用量 8l
rwxrwxrwx. 1 root root 26 1月 10 21:57 mysql -> /application/mysql-5.5.32/
drwxr-xr-x. 13 root root 4096 1月 10 21:54 mysql-5.5.32
lrwxrwxrwx. 1 root root 25 12月 19 03:30 nginx -> /application/nginx-1.6.3/
drwxr-xr-x. 11 root root 4096 12月 20 21:12 nginx-1.6.3
[root@lamp01 tools]#
操作到此步驟相當於編譯安裝make install 之後。
4、初始化資料庫
[root@lamp01 tools]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
解釋:
/application/mysql/scripts/mysql_install_db //指定安裝的命令
--basedir //指定mysql安裝的目錄
--datadir //存放數據文件的目錄
--user //mysql用戶
5、授權mysql管理資料庫文件
[root@lamp01 tools]# chown -R mysql.mysql /application/mysql/
[root@lamp01 tools]# cd /application/mysql/
[root@lamp01 mysql]# ll support-files/*.cnf
-rw-r--r--. 1 mysql mysql 4691 6月 19 2013 support-files/my-huge.cnf
-rw-r--r--. 1 mysql mysql 19759 6月 19 2013 support-files/my-innodb-heavy-4G.cnf
-rw-r--r--. 1 mysql mysql 4665 6月 19 2013 support-files/my-large.cnf
-rw-r--r--. 1 mysql mysql 4676 6月 19 2013 support-files/my-medium.cnf
-rw-r--r--. 1 mysql mysql 2840 6月 19 2013 support-files/my-small.cnf
[root@lamp01 mysql]#
6、生成mysql配置文件
\cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
7、配置啟動mysql
[root@lamp01 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe
[root@lamp01 mysql]# /application/mysql/bin/mysqld_safe &
[1] 1581
[root@lamp01 mysql]# 190110 22:29:53 mysqld_safe Logging to '/application/mysql/data/lamp0
1.err'.190110 22:29:53 mysqld_safe Starting mysqld daemon with databases from /application/mysql/
data
[root@lamp01 mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 1799 mysql 10u IPv4 21901 0t0 TCP *:mysql (LISTEN)
[root@lamp01 mysql]#
8、配置環境變數
vi /etc/profile
PATH="/application/mysql/bin:$PATH"
source /etc/profile
也可以把mysql命令放到已經有環境變數的路徑里
[root@lamp01 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@lamp01 mysql]# cp /application/mysql/bin/* /usr/local/sbin/
[root@lamp01 mysql]# which mysql
/usr/local/sbin/mysql
[root@lamp01 mysql]#
9、登陸測試
[root@lamp01 mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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>
出現以上提示符表示mysql已經安裝ok了。如果安裝時或者工作中有問題,可以看錯誤日誌分析問題原因:
cat /application/mysql/data/mysql-server.err
10、設置及更改mysql密碼
[root@lamp01 mysql]# mysqladmin -uroot password "123456"
[root@lamp01 mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@lamp01 mysql]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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@lamp01 mysql]# mysqladmin -uroot -p123456 password "bqh123"
[root@lamp01 mysql]# mysql -uroot -p123456
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@lamp01 mysql]# mysql -uroot -pbqh123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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@lamp01 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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>
11、安全優化:刪除不必要的庫和用戶
刪除test庫:dro database test;
刪除無用用戶(保留root和localhost)
drop user '用戶'@‘主機’;
註意:主機大寫或者特殊字元刪不了,需用
delete from mysql.user where user='用戶' and host='主機大寫或特殊字元';
如果不小心把這兩個也給刪除了,恢復方法:
grant all on *.* to ‘root’@localhostt identified by ‘密碼’ with grant option;
flush privileges; #刷新許可權
mysql簡單的命令:
查看所有庫:show databases;
切庫:use mysql;
查看用戶列表:select user,host from mysql.user
查看當前用戶:select user();
查看當前所在庫:select database();
刪除資料庫:drop database 庫名;
刪除用戶:drop user '用戶'@'主機';