欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

LNMP之Mysql源码编译安装

程序员文章站 2022-04-02 08:08:04
LNMP之Mysql源码编译安装...

LNMP之Mysql源码编译安装

Mysql

  1. 主流的sql数据库
  2. 官网:https://www.mysql.com/

Mysql编译安装脚本

yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel

useradd -s /sbin/nologin mysql

cd /usr/local/src

wget 'https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz'

tar -zxvf mysql-5.6.39.tar.gz

cd mysql-5.6.39

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1

make && make install

cp support-files/mysql.server /etc/init.d/mysqld

chmod a+x /etc/init.d/mysqld

编译选项说明

  1. CMACK_INSTALL_PREFIX指定安装的目录
  2. MYSQL_DATADIR指定Mysql的数据目录

mysql环境变量配置

export PATH=$PATH:/usr/local/mysql/bin/

mysql的启动步骤

  1. 更新配置
  2. 数据库初始化
  3. 启动数据库

更新mysql配置/etc/my.cnf

[mysqld]

bind-address=0.0.0.0

port=3306

datadir=/data/mysql

user=mysql

skip-name-resolve

long_query_time=2

slow_query_log_file=/data/mysql/mysql-slow.log

expire_logs_days=2

innodb-file-per-table=1

innodb_flush_log_at_trx_commit = 2

log_warnings = 1

max_allowed_packet      = 512M

connect_timeout = 60

net_read_timeout = 120

[mysqld_safe]

log-error=/data/mysql/mysqld.log

pid-file=/data/mysql/mysqld.pid

mysql数据库初始化

mkdir -pv /data/mysql

chown -R mysql:mysql  /usr/local/mysql /data/mysql/

yum install -y perl-Module-Install

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql  --datadir=/data/mysql/

使用systemctl管理mysqld,/usr/lib/systemd/system/mysqld.service

[Unit]

Description=mysqld

After=network.target

[Service]

Type=forking

ExecStart=/etc/init.d/mysqld start

[Install]

WantedBy=multi-user.target

启动mysql服务器

systemctl start mysqld

验证Mysql是否启动

查看进程:ps aux | grep mysql

查看监听:netstat -tulnp | grep mysql

查看日志:tail -f /data/mysql/mysqld.log

mysql的安全方式

  1. mysql能限制ip访问、访问需要用户名密码、mysql权限还能再细分
  2. 默认让127.0.0.1访问,用户名root,不需要密码
  3. mysql -uroot -h 127.0.0.1 -A

mysql加密码

  1. mysqladmin -h 127.0.0.1 -u root password 'zabbixpwd'
  2. mysql -h 127.0.0.1 -uroot -pzabbixpwd -A

mysql授权某ip登录

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.237.%' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTION;

flush privileges;

网络登录mysql

mysql -h 192.168.237.49 -uroot -pzabbixpwd -A



LNMP架构

  1. 用户 -> php文件
  2. 请求到达nginx,nginx通过127.0.0.1:9000去调用php-fpm
  3. php-fpm -> 运行php程序
  4. php -> 操作mysql

LNMP的环境验收,/usr/local/nginx/html/test_mysql.php

<?php

  $link=mysql_connect("127.0.0.1","root","zabbixpwd"); 

  if(!$link){

    echo "mysql_connect fail";

  }else{

    echo "mysql_connect success";

  }

?>


从PHP5.0开始就不推荐使用mysql_connect()函数,到了php7.0则直接废弃了该函数,替代的函数是:

mysqli_connect();

用法是:

$con=mysqli_connect("localhost","my_user","my_password","my_db");

官方的描述连接:http://php.net/manual/en/function.mysqli-connect.php
正确的测试代码:

<?PHP
    $conn=mysqli_connect("localhost","root","root");
    if($conn){
        echo"ok";
    }else{
        echo"error";    
    }
?>

 

 

本文地址:https://blog.csdn.net/lm19770429/article/details/109464538

相关标签: zabbix