趁着端午放假的時間,總結回顧一下在CentOS下搭建lamp環境的過程,還有針對mysql數據庫的一些操作。
LAMP 環境 :Linux + Apache + Mysql + PHP,這裏介紹我之前接觸過的yum安裝方式
1、安裝Apache
yum install httpd httpd-devel #httpd是插件 yum -y 代表默認yes
安裝完成後,啓動apache,執行命令:
/etc/init.d/httpd start(service httpd start) 前面是默認路徑
可以設爲開機啓動:
chkconfig httpd on
2、安裝mysql
yum install mysql mysql-server mysql-devel #3個端 mysql server devel
啓動mysql,執行命令:看是否開啓
# /etc/init.d/mysqld start (或 service mysqld start)
3、安裝php
yum install php php-mysql php-common php-gdphp-mbstring php-mcrypt php-devel php-xml
重啓apache
/etc/init.d/httpd restart
note:這個安裝包比較多,注意不要出錯
a.關閉防火牆
service iptables stop
b.重置加載防火牆
service iptables restart
4、檢查環境是否搭建成功
在/var/www/html/新建個test.php文件,將以下內容寫入,然後保存。
<?php
phpinfo();
?>
note:/var/www/html 的路徑下,才能在瀏覽器打開頁面
5、檢查Apache和mysql是否啓動
#打開Apache
service httpd start
#打開mysql
service mysqld start
#關閉防火牆
service iptables stop
如果要在本地打開頁面,就在地址欄輸入:虛擬機ip/test.php。顯示如下,表示安裝成功。
關於數據庫的一些操作:修改密碼,備份和還原。
修改密碼,先停止服務
# /etc/init.d/mysqld stop (或 service mysqld stop)
查看進程:
Ps -aux | grep mysql
mysqld_safe --user=mysql --skip-grant-tables --skip-networking #以安全模式進入
mysql -u root -p #用戶 root 密碼
mysql>show databases; #展示是否有數據庫
mysql>use mysql;
mysql>Show tables; #查看數據庫有哪些)有個user表
mysql>select user,password from user;
mysql>update user set Password=password('123456') where user='root';
mysql>flush privileges; #刷新緩存機制
mysql>quit; #退出數據庫
驗證--重啓mysql服務:
# /etc/init.d/mysqld restart
# mysql -u root -p
Enter password: <輸入新設的密碼newpassword>
mysql>
附:
yum命令:
yum update 升級系統
yum install ~ 安裝指定軟件包
yum update ~ 升級指定軟件包
yum remove ~ 卸載指定軟件
yum grouplist 查看系統中已經安裝的和可用的軟件組,可用的可以安裝
yum grooupinstall ~安裝上一個命令顯示的可用的軟件組中的一個
yum grooupupdate ~更新指定軟件組的軟件包
yum grooupremove ~ 卸載指定軟件組中的軟件包
yum deplist ~ 查詢指定軟件包的依賴關係
yum list yum\* 列出所有以yum開頭的軟件包
yum localinstall ~ 從硬盤安裝rpm包並使用yum解決依賴
數據庫備份與還原:
1)進入mysql路徑:cd /var/lib/mysql 會看到放在虛擬機上的數據庫
2)數據庫備份
mysqldump -u root -p密碼 數據庫名 數據表名 > mysql.sql #重定向給一個數據庫,可以任意命名,以.sql結尾
此時會在/var/lib/mysql目錄下自動生成一個mysql.sql備份文件
3)數據庫還原
mysql -u root -p 密碼 進入mysql控制檯‘mysql>’
查看數據庫:show databases;
建立要還原的數據庫:create database test2;
使用剛建立的數據庫:use test2;
導入數據:source test2.sql;
驗證還原是否成功,再次查看數據庫,show databases;
使用數據庫test2:use test2;
展示該所有數據庫表:show tables; 有表存在,還原成功。
最後提一句,如若要刪除數據庫(慎用):
drop database test2;