1. 程式人生 > >Linux MySql 安裝與配置

Linux MySql 安裝與配置

為什麼選擇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使用者

[[email protected] tools]# id mysql
id: mysql:無此使用者
[[email protected]

tools]# groupadd mysql
[[email protected] tools]# useradd -s /sbin/nologin -g mysql -M mysql
[[email protected] tools]# id mysql
uid=503(mysql) gid=503(mysql) 組=503(mysql)
[[email protected] tools]#

2、下載或上傳mysql二進位制軟體包並解壓

[[email protected] tools]# tar xf ./mysql-5.5.32-linux2.6-x86_64.tar.gz
[

[email protected] 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、將解壓的檔案移動到安裝目錄下,並做軟連線(隱藏版本號安全)

[[email protected] tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
[[email protected] tools]# ln -s /application/mysql-5.5.32/ /application/mysql
[[email protected] 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
[[email protected] tools]#

操作到此步驟相當於編譯安裝make install 之後。

4、初始化資料庫

[[email protected] 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管理資料庫檔案 

[[email protected] tools]# chown -R mysql.mysql /application/mysql/
[[email protected] tools]# cd /application/mysql/
[[email protected] 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
[[email protected] mysql]#
6、生成mysql配置檔案

\cp /application/mysql/support-files/my-small.cnf  /etc/my.cnf
7、配置啟動mysql

[[email protected] mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe
[[email protected] mysql]# /application/mysql/bin/mysqld_safe &
[1] 1581
[[email protected] 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
[[email protected] mysql]# lsof -i :3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  1799 mysql   10u  IPv4  21901      0t0  TCP *:mysql (LISTEN)
[[email protected] mysql]#

8、配置環境變數

vi /etc/profile

PATH="/application/mysql/bin:$PATH"

source /etc/profile

也可以把mysql命令放到已經有環境變數的路徑裡

[[email protected] mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] mysql]# cp /application/mysql/bin/* /usr/local/sbin/
[[email protected] mysql]# which mysql
/usr/local/sbin/mysql
[[email protected] mysql]#
9、登陸測試

[[email protected] 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密碼

[[email protected] mysql]# mysqladmin -uroot password "123456"
[[email protected] mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[[email protected] 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>

更改密碼:

[[email protected] mysql]# mysqladmin -uroot -p123456 password "bqh123"
[[email protected] mysql]# mysql -uroot -p123456
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[[email protected] 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>

為了安全起見,我們在登陸時,採用互動式登陸

[[email protected] 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 '使用者'@'主機';