爲何選擇MySQL數據庫?mysql
毫無疑問,絕大多數的使用linux操做系統的大中小型互聯網網站都在使用MySQL做爲其後端的數據庫存儲,從大型的BAT門戶,到電商平臺,分類門戶等無一例都使用MySQL數據庫。linux
My Sql 數據庫優勢:nginx
一、性能卓越,服務穩定,不多出現異常宕機sql
二、開放源代碼且無版權約束,自主性及使用成本低數據庫
三、歷史悠久,社區及用戶很是活躍,遇到問題,能夠尋求幫助後端
四、軟件體積小,安排使用簡單,而且易於維護,安裝及維護成本低安全
五、品牌口碑效應,使得企業無需考慮直接用之,LAMP,LEMP流行架構架構
六、支持多操做系統,提供多種API接口,支持多種開發語言,特別對流行的PHP語言有很好支持app
linux軟件的安裝方式:ide
一、 yum/rpm:簡單 快,沒法定製。
二、 編譯安裝:比較複雜,速度慢,可定製。
./configure;make;make install gmake;gmake insall
三、 二進制包*****
直接解壓就能用(相似於綠色軟件,無需安裝) 簡單,快,很差定製。
下面咱們選擇二進制包方法:
一、建立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]#
二、下載或上傳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
三、將解壓的文件移動到安裝目錄下,並作軟鏈接(隱藏版本號安全)
[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 以後。
四、初始化數據庫
[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用戶
五、受權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]#
六、生成mysql配置文件
\cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
七、配置啓動mysql
[root@lamp01 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /application/mysql/support-files/mysql.server
[root@lamp01 mysql]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld #將生成的啓動腳本拷貝到init.d目錄下
[root@lamp01 mysql]# chmod +x /etc/init.d/mysqld #授予可執行權限
[root@lamp01 mysql]# lsof -i:3306 #查詢mysql服務是否開啓
[root@lamp01 mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@lamp01 mysql]#
八、配置環境變量
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]#
九、登錄測試
[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
十、設置及更改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>
十一、安全優化:刪除沒必要要的庫和用戶
刪除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 '用戶'@'主機';