Django生產環境搭建

lm_y發表於2017-08-03
Django生產環境搭建本文為Django生產伺服器搭建手冊,用於本文所提到的需要下載的所有軟體包都可以在公共ftp://10.132.37.140/NXKTE/Cindy/CentOS6.7_packages/上找到,無需另行下載。1. Yum代理設定為yum新增代理設定,方便聯網安裝rpm軟體包[root@vm-vc02-SR1301 bin]# vi /etc/yum.conf寫入代理設定proxy=http://F1317065:TeWR5S7c@10.191.131.156:3128/2. 安裝Python3.4安裝所需的軟體包,請自行去ftp拷貝[root@vm-vc02-SR1301 ~]# ls -ltr-rw-r--r--. 1 root root 19554643 Nov 19 14:41 Python-3.4.3.tgz[root@vm-vc02-SR1301 ~]#1) 安裝環境安裝python前需提前安裝gcc,zlib-devel,openssl-devela. 安裝gcc[root@vm-vc02-SR1298 ~]# yum -y install gccb. 安裝zlib-devel[root@vm-vc02-SR1298 ~]# yum –y install zlib-develc. 安裝openssl-devel[root@vm-vc02-SR1298 ~]# yum -y install openssl-devel2) 安裝python 3.4a. 開始安裝建立安裝python3的資料夾[root@vm-vc02-SR1298 ~]# mkdir /usr/local/python3從官網下載指定環境的Python安裝包,為了統一開發環境,我們統一下載Python-3.4.3.tgz。[root@vm-vc02-SR1298 ~]# tar -zxvf Python-3.4.3.tgz[root@vm-vc02-SR1298 ~]# cd Python-3.4.3[root@vm-vc02-SR1298 Python-3.4.3]# ./configure --prefix=/usr/local/python3 --enable-shared --with-sslb. 編譯/安裝/清除檔案編譯[root@vm-vc02-SR1298 Python-3.4.3]# make all安裝[root@vm-vc02-SR1298 Python-3.4.3]# make install清除編譯的可執行檔案及配置檔案[root@vm-vc02-SR1298 Python-3.4.3]# make clean[root@vm-vc02-SR1298 Python-3.4.3]# make distcleanc. python 3.4安裝完成後的系統相關配置進入路徑[root@vm-vc02-SR1298 ~]# cd /etc/ld.so.conf.d建立python3.conf 檔案,寫入下面的內容並儲存關閉[root@vm-vc02-SR1298 ld.so.conf.d]# vi python3.conf/usr/local/python3/lib重新載入讓配置生效[root@vm-vc02-SR1298 ld.so.conf.d]# ldconfig檢視剛才安裝的python版本資訊[root@vm-vc02-SR1298 ~]# /usr/local/python3/bin/python3 -VPython 3.4.3[root@vm-vc02-SR1298 ~]# 因為CentOS自帶python2.6.6版本,系統預設的python版本是2.6.6.我們需要修改環境變數,指定系統預設的python版本為新安裝的python3 進入路徑/usr/bin[root@vm-vc02-SR1298 ~]# cd /usr/bin檢視當前系統指定的python所有版本[root@vm-vc02-SR1298 bin]# ls -ltr python*-rwxr-xr-x. 2 root root 4864 Jul 24 2015 python2.6-rwxr-xr-x. 2 root root 4864 Jul 24 2015 pythonlrwxrwxrwx. 1 root root 6 Feb 12 2016 python2 -> python[root@vm-vc02-SR1298 bin]#這裡可以看到,除了系統預設的python以外,還有一個名為python2的軟連線指向預設的python刪除指向預設python的軟連線[root@vm-vc02-SR1298 bin]# rm -rf python2將系統預設的python命令重新命名為python2.6.6[root@vm-vc02-SR1298 bin]# mv python python2.6.6此時/usr/bin下面已經沒有python這個可執行檔案了,在命令列下命令python已無法執行[root@vm-vc02-SR1298 bin]# ls -ltr python*-rwxr-xr-x. 2 root root 4864 Jul 24 2015 python2.6.6-rwxr-xr-x. 2 root root 4864 Jul 24 2015 python2.6 [root@vm-vc02-SR1298 bin]# python-bash: /usr/bin/python: No such file or directory[root@vm-vc02-SR1298 bin]#建立新的軟連線,將系統預設的python設為python3[root@vm-vc02-SR1298 bin]# ln -s /usr/local/python3/bin/python3.4 /usr/bin/python此時再在命令列下python,可以開啟python的直譯器,並且版本是新安裝的python3[root@vm-vc02-SR1298 bin]# pythonPython 3.4.3 (default, Nov 19 2016, 11:00:42) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linuxType "help", "copyright", "credits" or "license" for more information.>>>若想繼續使用python2,在命令列輸入python2.6或者python2.6.6即可[root@vm-vc02-SR1298 bin]# python2.6Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> exit()[root@vm-vc02-SR1298 bin]# python2.6.6Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> exit()[root@vm-vc02-SR1298 bin]#為了方便使用pip,為python3的pip設定環境變數[root@vm-vc02-SR1298 bin]# ln -s /usr/local/python3/bin/pip3.4 /usr/bin/pipd. 關於系統自帶的yum因為CentOS自帶的yum使用的python版本是python2.6X,所以python2.X版本我們繼續保留。這裡要修改yum配置檔案,將yum使用的python版本繼續指定回原來的python2.X.開啟yum執行檔案/usr/bin/yum,將標準頭部指向python2.6.6[root@vm-vc02-SR1298 bin]# vi /usr/bin/yum第一行的 #!/usr/bin/python 改為 #!/usr/bin/python2.6.6python3安裝完畢。3) 安裝Django-1.8安裝setuptools[root@vm-vc02-SR1298 bin]# export https_proxy=http://F1322817:YjC9vK9q@10.191.131.156:3128/ [root@vm-vc02-SR1298 bin]# pip install setuptoolsYou are using pip version 6.0.8, however version 9.0.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/python3/lib/python3.4/site-packages[root@vm-vc02-SR1298 bin]# pip install setuptools --upgrade安裝Django為了保持相同的開發環境,我們指定django版本為1.8.3並安裝[root@vm-vc02-SR1298 bin]# pip install django==1.8.3檢視django版本[root@vm-vc02-SR1298 bin]# pythonPython 3.4.3 (default, Nov 19 2016, 11:00:42) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import django>>> django.VERSION(1, 8, 3, 'final', 0)>>>4) 安裝連線資料庫的API安裝所需的軟體包,請自行去ftp拷貝[root@vm-vc02-SR1301 ~]# ls –ltr-rw-r--r--. 1 root root 2136329 Nov 19 14:45 freetds-stable.tgz-rw-r--r--. 1 root root 289838 Nov 19 15:49 oracle-instantclient-devel-10.2.0.4-1.x86_64.rpm-rw-r--r--. 1 root root 785363 Nov 19 15:49 oracle-instantclient-sqlplus-10.2.0.4-1.x86_64.rpm-rw-r--r--. 1 root root 34654975 Nov 19 15:49 oracle-instantclient-basic-10.2.0.4-1.x86_64.rpma. 安裝pymysqlPymysql - python3.X連線mysql的API安裝pymysql[root@vm-vc02-SR1298 bin]# pip install pymysql[root@vm-vc02-SR1298 bin]# pythonPython 3.4.3 (default, Nov 19 2016, 11:00:42) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import pymysql>>> exit()[root@vm-vc02-SR1298 bin]#在python3.4中pymysql取代了原來的MySQLdb,但在django-1.8仍使用的是MySQLdb。在新建的django專案中,更改專案目錄下的__init__.py檔案,加入以下程式碼,用pymysql代替MySQLdb:import pymysqlpymysql.install_as_MySQLdb()b. 安裝pymssqlpymssql - python3.X連線sql server的API安裝freetds[root@vm-vc02-SR1301 ~]# tar -zxvf freetds-stable.tgz[root@vm-vc02-SR1301 ~]# cd freetds-0.91[root@vm-vc02-SR1301 freetds-0.91]# ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --with-gnu-ld --enable-shared --enable-static[root@vm-vc02-SR1301 freetds-0.91]# make && make install[root@vm-vc02-SR1301 freetds-0.91]# echo "/usr/local/freetds/lib" >> /etc/ld.so.conf.d/freetds.conf[root@vm-vc02-SR1301 freetds-0.91]# ldconfig –v安裝pymssql[root@vm-vc02-SR1301 freetds-0.91]# pip install pymssql注意:如果不安裝freetds, 直接安裝pymssql會報如下錯誤:error: command 'gcc' failed with exit status 1c. 安裝cx_Oracle cx_Oracle - python3.X連線oracle的API安裝oracle客戶端及sqlplus[root@vm-vc02-SR1301 ~]# rpm -ivh oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm[root@vm-vc02-SR1301 ~]# rpm -ivh oracle-instantclient-devel-10.2.0.4-1.x86_64.rpm[root@vm-vc02-SR1301 ~]# rpm -ivh oracle-instantclient-sqlplus-10.2.0.4-1.x86_64.rpm建立sqlnet.ora和tnsnames.ora兩個檔案[root@vm-vc02-SR1301 ~]# cd /usr/lib/oracle/10.2.0.4/client64/[root@vm-vc02-SR1301 client64]# mkdir -p network/admin[root@vm-vc02-SR1301 client64]# vi /usr/lib/oracle/10.2.0.4/client64/network/admin/tnsnames.ora寫入配置EBUODB = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = 10.142.149.5) (PORT = 1903)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = SEBUODB) ) )配置環境變數Session級別配置[root@vm-vc02-SR1301 ~]# export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.4/client64/lib[root@vm-vc02-SR1301 ~]# export TNS_ADMIN=/usr/lib/oracle/10.2.0.4/client64/network/admin[root@vm-vc02-SR1301 ~]# export ORACLE_HOME=/usr/lib/oracle/10.2.0.4/client64環境變數配置[root@vm-vc02-SR1301 client64]# vi /root/.bash_profile在export PATH前新增三行TNS_ADMIN=/usr/lib/oracle/10.2.0.4/client64/network/adminLD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.4/client64/libPATH=$PATH:$HOME/bin:/usr/lib/oracle/10.2.0.4/client64/bin在/etc/ld.so.conf新增oracle配置[root@vm-vc02-SR1301 client64]# vi /etc/ld.so.conf新增一行/usr/lib/oracle/12.1/client64/lib/[root@vm-vc02-SR1301 client64]# ldconfig安裝cx_Oracle[root@vm-vc02-SR1301 ~]# pip install cx_Oracle[root@vm-vc02-SR1301 ~]# pythonPython 3.4.3 (default, Nov 19 2016, 14:43:21) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import cx_Oracle>>> exit()[root@vm-vc02-SR1301 ~]#3. 安裝Apache 2.4安裝所需的軟體包,請自行去ftp拷[root@vm-vc02-SR1301 ~]# ls -ltr-rw-r--r--. 1 root root 1031613 Nov 19 15:04 apr-1.5.2.tar.gz-rw-r--r--. 1 root root 874044 Nov 19 15:04 apr-util-1.5.4.tar.gz-rw-r--r--. 1 root root 2041593 Nov 19 15:05 pcre-8.37.tar.gz-rw-r--r--. 1 root root 7051797 Nov 19 15:08 httpd-2.4.18.tar.gz[root@vm-vc02-SR1301 ~]#1) 編譯工具準備需安裝gcc,gcc-c++安裝python之前已經安裝過gcc,這裡只需要再安裝gcc-c++[root@vm-vc02-SR1301 ~]# yum install gcc-c++才對2) 安裝依賴庫解壓放置apr和apr-util[root@vm-vc02-SR1301 ~]# mkdir /usr/local/httpd[root@vm-vc02-SR1301 ~]# tar zxvf apr-1.5.2.tar.gz -C /usr/local/httpd/[root@vm-vc02-SR1301 ~]# tar -zxvf apr-util-1.5.4.tar.gz -C /usr/local/httpd/[root@vm-vc02-SR1301 ~]# tar -zxvf httpd-2.4.18.tar.gz -C /usr/local/httpd/[root@vm-vc02-SR1301 ~]# cd /usr/local/httpd[root@vm-vc02-SR1301 httpd]# ls -ltrtotal 12drwxr-xr-x. 19 1000 1000 4096 Sep 17 2014 apr-util-1.5.4drwxr-xr-x. 27 1000 1000 4096 Apr 25 2015 apr-1.5.2drwxr-xr-x. 11 501 games 4096 Dec 9 2015 httpd-2.4.18[root@vm-vc02-SR1301 httpd]# mv apr-1.5.2 httpd-2.4.18/srclib/apr[root@vm-vc02-SR1301 httpd]# mv apr-util-1.5.4 httpd-2.4.18/srclib/apr-util[root@vm-vc02-SR1301 httpd]# ls -ltrtotal 4drwxr-xr-x. 11 501 games 4096 Dec 9 2015 httpd-2.4.18[root@vm-vc02-SR1301 httpd]#編譯安裝pcre[root@vm-vc02-SR1301 httpd]# mkdir /usr/local/pcre回到伺服器存放軟體包的路徑,解壓縮pcre檔案[root@vm-vc02-SR1301 ~]# tar -zxvf pcre-8.37.tar.gz[root@vm-vc02-SR1301 ~]# cd pcre-8.37[root@vm-vc02-SR1301 pcre-8.37]# ./configure --prefix=/usr/local/pcre[root@vm-vc02-SR1301 pcre-8.37]# make && make install3) 編譯安裝apache(httpd)建立apache資料夾[root@vm-vc02-SR1301 ~]# mkdir /usr/local/apache編譯安裝apache[root@vm-vc02-SR1301 ~]# cd /usr/local/httpd/httpd-2.4.18/[root@vm-vc02-SR1301 httpd-2.4.18]# ./configure --prefix=/usr/local/apache --with-included-apr --with-pcre=/usr/local/pcre[root@vm-vc02-SR1301 httpd-2.4.18]# make && make install4) 測試apache啟動apache伺服器[root@vm-vc02-SR1301 bin]# /usr/local/apache/bin/apachectl start為了在本機測試apache,我們為伺服器安裝firefox瀏覽器[root@vm-vc02-SR1301 bin]# yum -y install firefox開啟firefox瀏覽器[root@vm-vc02-SR1301 bin]# firefox&在位址列輸入localhost,如果看到如下畫面,則apache已成功安裝 5) 配置apache開機啟動複製apache安裝目錄下的apachectl到/etc/init.d/目錄下[root@vm-vc02-SR1301 ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd在/etc/init.d/httpd配置檔案新增下面兩行內容[root@vm-vc02-SR1301 ~]# vi /etc/init.d/httpd#chkconfig:345 85 15##description:Start and Stop the Apache HTTP Server新增httpd到系統服務[root@vm-vc02-SR1301 ~]# chkconfig --add httpd設定為開機啟動[root@vm-vc02-SR1301 ~]# chkconfig httpd on檢查是否設定成功:執行下面的命令,返回的2.3.4.5相為on則代表設定成功[root@vm-vc02-SR1301 ~]# chkconfig --list httpdhttpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@vm-vc02-SR1301 ~]#6) 設定防火牆我們的伺服器對外開放80,8080,90,9090埠,設定允許以上埠通過防火牆[root@vm-vc02-SR1301 ~]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT;[root@vm-vc02-SR1301 ~]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT;[root@vm-vc02-SR1301 ~]# /sbin/iptables -I INPUT -p tcp --dport 90 -j ACCEPT;[root@vm-vc02-SR1301 ~]# /sbin/iptables -I INPUT -p tcp --dport 9090 -j ACCEPT;[root@vm-vc02-SR1301 ~]# /etc/rc.d/init.d/iptables save;[root@vm-vc02-SR1301 ~]# service iptables status4. 安裝mod_wsgi安裝所需的軟體包,請自行去ftp拷貝[root@vm-vc02-SR1301 ~]# ls -ltr-rw-r--r--. 1 root root 929310 Nov 19 15:49 mod_wsgi-4.4.21.tar.gz安裝mod_wsgi[root@vm-vc02-SR1301 ~]# tar -zxvf mod_wsgi-4.4.21.tar.gz[root@vm-vc02-SR1301 ~]# cd mod_wsgi-4.4.21[root@vm-vc02-SR1301 mod_wsgi-4.4.21]# ./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/python3/bin/python3[root@vm-vc02-SR1301 mod_wsgi-4.4.21]# make && make install修改apache配置檔案,寫入mod_wsgi配置[root@vm-vc02-SR1301 conf]# vi /usr/local/apache/conf/httpd.conf新增一行LoadModule wsgi_module modules/mod_wsgi.so注意:httpd.conf修改錯誤會導致apache服務無法啟動,修改前做好備份。5. 安裝MySQL 安裝所需的軟體包,請自行去ftp拷貝[root@vm-vc02-SR1301 ~]# ls -ltr-rw-r--r--. 1 root root 4579502 Nov 19 15:50 MySQL-devel-5.6.21-1.rhel5.x86_64.rpm-rw-r--r--. 1 root root 23156366 Nov 19 15:50 MySQL-client-5.6.21-1.rhel5.x86_64.rpm-rw-r--r--. 1 root root 88524802 Nov 19 15:50 MySQL-server-5.6.21-1.rhel5.x86_64.rpm [root@vm-vc02-SR1301 ~]#1) 安裝MySQL解除安裝CentOS自帶的mysql[root@vm-vc02-SR1301 ~]# rpm -qa | grep mysqlmysql-libs-5.1.73-5.el6_6.x86_64[root@vm-vc02-SR1301 ~]# yum -y remove mysql-libs按以下的順序依次安裝rpm包[root@vm-vc02-SR1301 ~]# rpm -ivh MySQL-server-5.6.21-1.rhel5.x86_64.rpm[root@vm-vc02-SR1301 ~]# rpm -ivh MySQL-devel-5.6.21-1.rhel5.x86_64.rpm[root@vm-vc02-SR1301 ~]# rpm -ivh MySQL-client-5.6.21-1.rhel5.x86_64.rpm2) 配置MySQL啟動檔案拷貝mysql配置檔案到/etc下[root@vm-vc02-SR1301 ~]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf修改 /etc/my.cnf,寫入如下藍色配置# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html[client]password = 123456port = 3306default-character-set=utf8[mysqld]port = 3306character_set_server=utf8character_set_client=utf8collation-server=utf8_general_cilower_case_table_names=0max_connections=1000skip-name-resolveevent_scheduler=ONmax_allowed_packet=1000M# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....# socket = .....# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES[mysql]default-character-set=utf83) 設定防火牆允許mysql預設埠3306通過防火牆[root@vm-vc02-SR1301 ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT;[root@vm-vc02-SR1301 ~]# /etc/rc.d/init.d/iptables save;[root@vm-vc02-SR1301 ~]# service iptables status4) 初始化MySQL初始化mysql[root@vm-vc02-SR1301 ~]# /usr/bin/mysql_install_db啟動mysql[root@vm-vc02-SR1301 ~]# service mysql start5) 設定root密碼MySQL安裝完成後,因為沒有建立root密碼,初次登入要以mysql的安全模式登入,並設定root初始密碼的為123。在當前的控制檯輸入以下命令[root@vm-vc02-SR1301 ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &開啟另外一個控制檯,用root賬號免密碼登入mysql,修改root賬號的密碼[root@vm-vc02-SR1301 ~]# mysql -u root mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use mysqlDatabase changedmysql> update user set password=PASSWORD('123') where user='root';Query OK, 4 rows affected (0.04 sec)Rows matched: 4 Changed: 4 Warnings: 0mysql> flush privileges;Query OK, 0 rows affected (0.02 sec)mysql>exit[root@vm-vc02-SR1301 ~]#密碼修改完成後,停止mysql退出安全模式,並再次啟動,讓MySQL回到正常模式。[root@vm-vc02-SR1301 ~]# service mysql stop[root@vm-vc02-SR1301 ~]# service mysql start注意:這一步不可省略為service mysql restart, 否則mysql重啟後仍然是在安全模式下。用root/123登入mysql,修改root賬號的密碼[root@vm-vc02-SR1301 ~]# mysql -u root -pEnter password: 123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.21Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set PASSWORD = PASSWORD('foxconn168!');Query OK, 0 rows affected (0.01 sec)mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set password = PASSWORD('foxconn168!') where user = 'root';Query OK, 3 rows affected (0.00 sec)Rows matched: 4 Changed: 3 Warnings: 0mysql> exitBye[root@vm-vc02-SR1301 ~]#至此,root的密碼成功修改為foxconn168!, root使用者從本地可成功登入mysql。6) 建立其他賬號MySQL賬號分本地賬號和網路賬號,本地賬號只能從本地連線mysql,網路賬號可以從網路上的其他伺服器遠端連線mysql。用root賬號登入mysql,建立賬號cindy,密碼123,並賦予本地和遠端兩種許可權。[root@vm-vc02-SR1301 mysql]# mysql -u root -pEnter password: foxconn168!Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> grant all privileges on *.* to cindy@"%" identified by "123";#賬號:cindy,密碼123,%為網路訪問許可權Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> grant all privileges on *.* to cindy@"localhost" identified by "123";#賬號:cindy,密碼123,localhost為網路訪問許可權Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> exitBye此時,cindy/123賬號可以從本機連線mysql[root@vm-vc02-SR1301 mysql]# mysql -u cindy -pEnter password: 123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>從其他伺服器遠端連線mysql[root@vm-vc02-SR910 bin]# mysql -h 10.129.4.153 -u cindy -pEnter password: 123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> 7) 設定開機啟動[root@vm-vc02-SR1301 ~]# chkconfig mysql on[root@vm-vc02-SR1301 ~]# chkconfig --list | grep mysql8) 修改資料庫檔案存放路徑隨著業務量的增長,MySQL預設安裝資料檔案存放位置分割槽大小出現瓶頸,通常需要將datadir換到較大的分割槽。MySQL預設安裝資料檔案存放位置/var/lib/mysql修改MySQL資料檔案存放路徑到擴充套件分割槽/mysite/data/mysql下:a) 關閉MySQL[root@vm-vc02-SR1301 ~]# service mysql stopb) 建立存放mysql資料的資料夾並設定屬性[root@vm-vc02-SR1301 ~]# mkdir -p /mysite/data/mysql/[root@vm-vc02-SR1301 ~]# chown mysql:mysql /mysite/data/mysqlc) 移動mysql原始資料檔案到新建立的資料夾[root@vm-vc02-SR1301 ~]# mv /var/lib/mysql/* /mysite/data/mysql/d) 修改mysql配置檔案/etc/my.cnf修改# datadir = ..... 為datadir = /mysite/data/mysql/修改# socket = .....為socket = /mysite/data/mysql/mysql.socke) 檢查SELinux狀態(CentOS必須)[root@vm-vc02-SR1301 ~]# getenforce若收到的返回值為”Permissive”或者”Disabled”,則此步驟結束,直接進入下一步。若收到的返回值為”Enforcing”,則調整SELinux引數以接受MySQL配置的更新:安裝policycoreutils-python[root@vm-vc02-SR1301 ~]# yum provides /usr/sbin/semanage[root@vm-vc02-SR1301 ~]# yum whatprovides /usr/sbin/semanage[root@vm-vc02-SR1301 ~]# yum -y install policycoreutils-python這裡要修改semanage配置檔案/usr/sbin/semanage,檔案頭改成python2.X.#! /usr/bin/python -Es改為#! /usr/bin/python2.6.6 –Es執行semanage命令,為新的mysql資料檔案路徑新增上下文對映[root@vm-vc02-SR1301 ~]# semanage fcontext -a -t mysqld_db_t "/mysite/data/mysql(/.*)?"使上面的對映生效[root@vm-vc02-SR1301 ~]# restorecon -Rv /mysite/data/mysqlf) 啟動MySQL[root@vm-vc02-SR1301 ~]# service mysql startg) 檢視mysql程式,檢查配置是否生效[root@vm-vc02-SR1301 ~]# ps -ef | grep mysqlroot 10304 1 0 13:54 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/mysite/data/mysql/ --pid-file=/mysite/data/mysql//vm-vc02-SR910.pidmysql 10651 10304 0 13:54 pts/0 00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/mysite/data/mysql/ --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/mysite/data/mysql//vm-vc02-SR910.err --pid-file=/mysite/data/mysql//vm-vc02-SR910.pid --socket=/mysite/data/mysql/mysql.sock --port=3306root 10710 3269 0 14:32 pts/0 00:00:00 grep mysql[root@vm-vc02-SR1301 ~]#h) 操作驗證登入mysql,新建資料庫/新建表進行驗證,操作都順利完成的話,mysql資料檔案路徑修改完成。參考文獻:http://crashmag.net/change-the-default-mysql-data-directory-with-selinux-enabled6. 安裝crond1) 安裝crond[root@vm-vc02-SR1299 software]# yum -y install vixie-cron2) 設定開機啟動[root@ vm-vc02-SR1301 software]# chkconfig crond on[root@ vm-vc02-SR1301 software]# chkconfig --list | grep crond

相關文章