您的位置:首页 > 数据库 > MySQL

马哥教育第二十三MySQL基础应用入门

2015-05-28 19:56 731 查看
1、MySQL基础应用入门
安装mariadb-10.0.19:
vim /usr/local/mariadb-10.0.19-linux-x86_64/INSTALL-BINARY
cd /usr/local
ln -sv mariadb-10.0.19-linux-x86_64 mysql
chown -R root:mysql .
mkdir /etc/mysql

cp ./support-files/my-innodb-heavy-4G.cnf /etc/mysql/my.cnf:复制文件 datadir=/mydata/data:添加指定目录数据存储到/data/mydata目录
scripts/mysql_install_db --user=mysql --datadir=/mydata/data:指定用户和路径初始化数据库,其他参数查看--help
cp support-files/mysql.server /etc/init.d/mysqld:复制启动脚本 chkconfig --add mysqld chkconfig --level 2345 mysqld on chkconfig --list mysqld /usr/local/mysql/bin/mysqladmin -u root password 'XXXXXX': service mysqld start
create database phpdb; create user phpuser@'192.168.%.%' identified by 'XXXXXX' grant all on phpdb.* to phpuser@'192.168.%.%';
查看support-files目录下支持文件
根据不同内存大小选用不同配置文件(my-small.cnf、my-large.cnf、my-huge.cnf、my-innodb-heavy-4G.cnf)
sql数据类型:
字符型:固定字符型char、可变长度varchar
数值型:精确数值型:int、近似数值型:float、double
日期型:date、time、datetime、year(4)、year(2)
内置型:enum、set
类型修饰:null、not null、default
数值型:unsigned:无符号
整数:auto_increment

ddl:caeate {database,table,user}、drop、alter、grant、revoke

dml:insert、delete、update、select

数据库语句:
create datebases db_name;
drop datebases db_name;
show databases;
create tables [db_name.]tb_name(字段1 字段类型或修饰类型,字段2 字段类型或修饰类型,......);
drop tables tb_name;
desc tb_name;显示表的结构

create user test@'192.168.%.%' identified by '123456';
%表示任意长度任意字符;
_匹配任意长度单个字符;
mysql -utest -p:本地连接
mysql -utest -h 192.168.146.138 -p:远程连接
drop user test@'192.168.%.%';

grant all on testphp.* to test@'192.168.%.%';授权
grant all on testphp.test to test@'192.168.%.%';授权单表
all 表示所有权限。
db_name *表示所有库。
tb_name *表示所有表。
flush privileges; 授权立即生效。

revoke all on testpho.* from test@'192.168.%.%';

insert into pw_userapp values(1,12321,xj,123,343);插入数据

2、MySQL基础应用及编译LAMP
delete from pw_userapp;
select * from pw_userapp;
select user,host,password from user where user='root' order by host desc;倒序查找数据
update pw_userapp set user='test' where id='3';
BETWEEN start_value AND end_value;
LIKE: 模糊匹配:Name LIKE O%;
rlike:支持正则表达式;
select user,host,password from mysql.user where user rlike '^p' order by host desc;

mysql常用的show命令
show engines;查询mysql存储引擎。
show table status \G:查看当前库中标的详细信息,不需要使用分号结尾
show character set;显示字符集
show collation;显示字符集排序规则
show global variables;mysql各种工作属性是通过其服务器变量来定义的:
show global variables like '%wait_timeout%';查看单个变量中的值
show global status;显示数据库中所有状态统计数据
show global status like '%Uptime%';显示统计单个变量数据

编译安装php2.6(httpd和php在同一台服务器上安装配置)
请配置好yum源(系统安装源及epel源)后执行如下命令:
yum -y groupinstall "Desktop Platform Development"
yum -y install bzip2-devel libmcrypt-devel
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
参数说明:
--prefix=/usr/local/php:安装路径
--with-mysql=/usr/local/mysql:编译使用mysql驱动指明路径,如果是rpm包安装的mysql只需写--with-mysql
--with-openssl: --with-mysql的加密工具
--with-mysqli=/usr/local/mysql/bin/mysql_config:php访问mysql的另外一种机制
--enable-mbstring:多字节字串,如果想支持中文这个选项是必须
--with-freetype-dir:字体处理工具,php显示网页可以对字体加工。
--with-jpeg-dir :jpeg图片生成工具
--with-png-dir:png图片生成工具
--with-zlib:压缩库
--with-libxml-dir=/usr:指明需要使用的xml库所在位置,
--enable-xml:启用xml功能
--enable-sockets:支持sockets方式进行通信
--with-apxs2=/usr/local/apache/bin/apxs:apxs2为http函数用于编译http第三方模块,如果需要把php编译成模块就必须说明,模块编译函数在那里。
--with-mcrypt:支持使用加密库
--with-config-file-path=/etc:在etc下创建主配置文件php.ini
--with-config-file-scan-dir=/etc/php.d:支持主配置文件以外在php.d下的以.ini结尾子配置文件,
--with-bz2:支持bz2压缩
--enable-maintainer-zts:如果http编译时指明event、worker这项就必须添加,如果:frefork模式这项不能添加。
这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

为php提供配置文件:
cp php.ini-development /etc/php.ini:开发环境可以使用此配置文件
cp php.ini-production /etc/php.ini:生产环境用此配置文件

编辑httpd配置文件,让httpd支持php模块:
vim /etc/httpd/httpd.conf :http主配置文件。
添加如下二行:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html :
DirectoryIndex index.php index.html
重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。

测试页面index.php示例如下:
<?php
$link = mysql_connect('127.0.0.1','root','mageedu');
if ($link)
echo "Success...";
else
echo "Failure...";
mysql_close();
?>

3、php-fpm模式下的LAMP
在httpd2.4版本中查看服务器状态页面需要添加
<Location /server-status>
SetHandler server-status
Require all granted
</Location>

fpm安装php(php单独运行不以模块方式运行)
tar xf php-5.6.9.tar.bz2
cd php-5.6.9 ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
--prefix=/usr/local/php:安装路径
--with-mysql=/usr/local/mysql:编译使用mysql驱动指明路径,如果是rpm包安装的mysql只需写--with-mysql
--with-openssl: --with-mysql的加密工具
--with-mysqli=/usr/local/mysql/bin/mysql_config:php访问mysql的另外一种机制
--enable-mbstring:多字节字串,如果想支持中文这个选项是必须
--with-freetype-dir:字体处理工具,php显示网页可以对字体加工。
--with-jpeg-dir :jpeg图片生成工具
--with-png-dir:png图片生成工具
--with-zlib:压缩库
--with-libxml-dir=/usr:指明需要使用的xml库所在位置,
--enable-xml:启用xml功能
--enable-sockets:支持sockets方式进行通信
--enable-fpm:以fpm方式运行
--with-mcrypt:支持使用加密库,如果不使用该项对某些需要加密的功能会报错。
--with-config-file-path=/etc:在etc下创建主配置文件php.ini
--with-config-file-scan-dir=/etc/php.d:支持主配置文件以外在php.d下的以.ini结尾子配置文件,
--with-bz2:支持bz2压缩

启用mod_proxy_fcgi.so必须使用代理模块proxy_module,把以下2个注释去掉即可: LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so LoadModule proxy_module modules/mod_proxy.so 启用反向代理: ProxyRequests Off:关闭正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.146.141:9000/$1:所有以.php结尾的网页都以fcgi方式转到后面网页
例如:
<VirtualHost *:80>
DocumentRoot "/www/magedu.com"
ServerName magedu.com
ServerAlias www.magedu.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/magedu.com/$1
<Directory "/www/magedu.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 基础应用 mariadb