爲何選擇MySQL數據庫?mysql
毫無疑問,絕大多數的使用linux操做系統的大中小型互聯網網站都在使用MySQL做爲其後端的數據庫存儲,從大型的BAT門戶,到電商平臺,分類門戶等無一例都使用MySQL數據庫。linux
My Sql 數據庫優勢:sql
一、性能卓越,服務穩定,不多出現異常宕機數據庫
二、開放源代碼且無版權約束,自主性及使用成本低後端
三、歷史悠久,社區及用戶很是活躍,遇到問題,能夠尋求幫助安全
四、軟件體積小,安排使用簡單,而且易於維護,安裝及維護成本低架構
五、品牌口碑效應,使得企業無需考慮直接用之,LAMP,LEMP流行架構app
六、支持多操做系統,提供多種API接口,支持多種開發語言,特別對流行的PHP語言有很好支持socket
linux軟件的安裝方式:tcp
一、 yum/rpm:簡單 快,沒法定製。
二、 編譯安裝:比較複雜,速度慢,可定製。
./configure;make;make install gmake;gmake insall
三、 二進制包*****
直接解壓就能用(相似於綠色軟件,無需安裝) 簡單,快,很差定製。
測試環境:
系統:Centos 6.5 mysql軟件:mysql-5.1.72.tar.gz
下面咱們選擇編譯安裝方法:
一、建立mysql用戶
[root@bqh-119 ~]# groupadd mysql [root@bqh-119 ~]# useradd mysql -g mysql -M -s /sbin/nologin [root@bqh-119 ~]# id mysql uid=502(mysql) gid=502(mysql) 組=502(mysql)
二、下載或上傳mysql軟件包並解壓
獲取mysql軟件:https://dev.mysql.com/downloads/mysql/
[root@bqh-119 tools]# tarxf mysql-5.1.72.tar.gz [root@bqh-119 tools]# ll 總用量 13328 drwxrwxrwx 32 7155 wheel 4096 7月 18 21:55 mysql-5.1.72 -rw-r--r-- 1 root root 24044338 7月 18 21:41 mysql-5.1.72.tar.gz [root@bqh-119 tools]# cd mysql-5.1.72 [root@bqh-119 mysql-5.1.72]#
三、編譯安裝mysql
[root@bqh-119 mysql-5.1.72]# ./configure \ --prefix=/application/mysql5.1.72 \ --with-unix-socket-path=/application/mysql5.1.72/tmp/mysql.sock \ --localstatedir=/application/mysql5.1.72/data \ --enable-assembler \ --enable-thread-safe-client \ --with-mysqld-user=mysql \ --with-big-tables \ --without-debug \ --with-pthread \ --enable-assembler \ --with-extra-charsets=complex \ --with-readline \ --with-ssl \ --with-embedded-server \ --enable-local-infile \ --with-plugins=partition,innobase \ --with-mysqld-ldflags=-all-static \ --with-client-ldflags=-all-static 此處省略...... checking for nl_langinfo and CODESET... yes checking for wchar_t in wchar.h... yes checking for wctype_t in wctype.h... yes checking for wint_t in wctype.h... yes checking for tgetent in -lncursesw... no checking for tgetent in -lncurses... no checking for tgetent in -lcurses... no checking for tgetent in -ltermcap... no checking for tgetent in -ltinfo... no checking for termcap functions library... configure: error: No curses/termcap library found
上面報錯解決方法:
[root@bqh-119 mysql-5.1.72]# yum -y install ncurses-devel
四、而後再重新編譯一下,出現以下圖就能夠接着make && make install
五、而後再執行:
[root@bqh-119 mysql-5.1.72]# make && make install #make靜態編譯生成mysqld的執行文件;make install安裝mysql
提示:也能夠根據cpu核數來加-j參數來加快mysql的編譯,例如:make -j 4
出現下圖結果表示mysql安裝完成:
六、作軟鏈接(隱藏版本號安全)
[root@bqh-119 mysql-5.1.72]# ln -s /application/mysql5.1.72/ /application/mysql [root@bqh-119 mysql-5.1.72]# ll /application/mysql lrwxrwxrwx 1 root root 25 7月 18 22:27 /application/mysql -> /application/mysql5.1.72/ [root@bqh-119 mysql-5.1.72]# ll /application/mysql/ 總用量 28 drwxr-xr-x 2 root root 4096 7月 18 22:22 bin drwxr-xr-x 3 root root 4096 7月 18 22:22 include drwxr-xr-x 3 root root 4096 7月 18 22:22 lib drwxr-xr-x 2 root root 4096 7月 18 22:22 libexec drwxr-xr-x 10 root root 4096 7月 18 22:22 mysql-test drwxr-xr-x 6 root root 4096 7月 18 22:22 share drwxr-xr-x 5 root root 4096 7月 18 22:22 sql-bench
七、初始化數據庫:
[root@bqh-119 mysql-5.1.72]# /application/mysql/bin/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
解釋:
/application/mysql/bin/mysql_install_db #指定安裝的命令
--basedir #指定mysql安裝的目錄
--datadir #存放數據文件的目錄
--user #mysql用戶
八、受權mysql管理數據庫文件:
[root@bqh-119 mysql-5.1.72]# chown -R mysql.mysql /application/mysql/
九、生成mysql配置文件:
[root@bqh-119 mysql-5.1.72]# ll /application/mysql/share/mysql/*.cnf -rw-r--r-- 1 mysql mysql 4746 7月 18 22:22 /application/mysql/share/mysql/my-huge.cnf -rw-r--r-- 1 mysql mysql 19779 7月 18 22:22 /application/mysql/share/mysql/my-innodb-heavy-4G.cnf -rw-r--r-- 1 mysql mysql 4720 7月 18 22:22 /application/mysql/share/mysql/my-large.cnf -rw-r--r-- 1 mysql mysql 4731 7月 18 22:22 /application/mysql/share/mysql/my-medium.cnf -rw-r--r-- 1 mysql mysql 2499 7月 18 22:22 /application/mysql/share/mysql/my-small.cnf [root@bqh-119 mysql-5.1.72]# \cp /application/mysql/share/mysql/my-small.cnf /etc/my.cnf
十、配置啓動mysql:
[root@bqh-119 mysql-5.1.72]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /application/mysql/share/mysql/mysql.server [root@bqh-119 mysql-5.1.72]# cp /application/mysql/share/mysql/mysql.server /etc/init.d/mysqld #將生成的啓動腳本拷貝到init.d目錄下 [root@bqh-119 mysql-5.1.72]# chmod +x /etc/init.d/mysqld #授予可執行權限 [root@bqh-119 mysql-5.1.72]# lsof -i:3306 [root@bqh-119 mysql-5.1.72]# /etc/init.d/mysqld start #啓動mysql服務 Starting MySQL. SUCCESS! [root@bqh-119 mysql-5.1.72]# lsof -i:3306 #查詢mysql服務是否開啓 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 51239 mysql 10u IPv4 65387 0t0 TCP *:mysql (LISTEN)
十一、配置環境變量:
[root@bqh-119 mysql-5.1.72]# echo -e '#mysql \nPATH="/application/mysql/bin:$PATH"' >>/etc/profile [root@bqh-119 mysql-5.1.72]# source /etc/profile [root@bqh-119 mysql-5.1.72]# tail -2 /etc/profile #mysql PATH="/application/mysql/bin:$PATH" [root@bqh-119 mysql-5.1.72]# which mysql /application/mysql/bin/mysql
十二、登錄測試及設置、更改mysql密碼:
[root@bqh-119 mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.72 Source distribution 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> quit Bye [root@bqh-119 mysql]# mysqladmin -uroot password "123456" #設置密碼 [root@bqh-119 mysql]# mysql -uroot -p123456 #密碼登陸 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.72 Source distribution 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> quit Bye
1三、爲了安全起見,咱們在登錄時,採用交互式登錄:
[root@bqh-119 mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.72 Source distribution 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@bqh-119 ~]# killall mysqld [root@bqh-119 ~]# killall mysqld -9 mysqld: 沒有進程被殺死 [root@bqh-119 ~]# /application/mysql/bin/mysqld_safe --skip-grant-table & [1] 51534 [root@bqh-119 ~]# 190719 00:41:07 mysqld_safe Logging to '/application/mysql5.1.72/data/bqh-119.err'. 190719 00:41:07 mysqld_safe Starting mysqld daemon with databases from /application/mysql5.1.72/data [root@bqh-119 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.72 Source distribution 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> update mysql.user set password=PASSWORD("bqh123") where host='localhost' and user='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@bqh-119 ~]# killall mysqld [root@bqh-119 ~]# killall mysqld mysqld: 沒有進程被殺死 [root@bqh-119 ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [root@bqh-119 ~]# mysql -uroot -pbqh123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.1.72 Source distribution 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@bqh-119 ~]# mysqladmin -uroot -pbqh123 shutdown [root@bqh-119 ~]# [root@bqh-119 ~]# netstat -lntup|grep mysql [root@bqh-119 ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [root@bqh-119 ~]# netstat -lntup|grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 51970/mysqld
1四、安全優化:刪除沒必要要的庫和用戶
刪除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 '用戶'@'主機';