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
Django生产环境搭建
最新推荐文章于 2022-11-06 18:35:29 发布