Macbook安装mysql+php+phpmyadming进行网站构建笔记

最近在学PHP,需要搭建LAMP,但是自己的机器是Macbook,为了学习就在上面搭建,经理很曲折。

一、环境搭建

    1、硬件:macbook air(2013年)i5

    2、系统:macos majave 10.14

    3、软件:

          apache:Apache/2.4.34 (Unix)---系统自带;

          mysql:8.0.19    下载地址:https://dev.mysql.com/downloads/mysql/

          workbench:8.0.19 下载地址:https://dev.mysql.com/downloads/workbench/  (workbench是mysql操作数据库的界面环境,一定要注意版本一致,之前用的不一致的版本就是连不上数据库,恼怒的好几天!

          PHP:版本为7.1.19   。macbook上会默认安装php,通过设置/etc/apache2/httpd.conf文件,#LoadModule php7_module libexec/apache2/libphp7.so的语句中删除“#”,重新运行sudo apachectl restart即可。

          phpmyadmin:4.8.3多国语言版本 https://www.phpmyadmin.net/files/4.8.3/,下载后解压,放到/Library/WebServer/Documents(apache发布网站的位置)目录下,文件夹命名为phpmyadmin。

 备注:macos系统比较好的是可以利用命令行,与linux系统部分程序兼容,可以通过brew clone 操作进行下载,有兴趣的人可以百度一下。

二、遇坑解决方案

     1、收件解决就是workbench连接数据库问题,一方面,版本不一致,修改后还是不行,具体错误找不到,但是记得添加了 /private/etc/my.cnf文件,内容如下:

[client]
  default-character-set=utf8
 
#password   =root password
  port        = 3306
  socket      = /tmp/mysql.sock
  # Here follows entries for some specific programs
  # The MySQL server
  [mysqld]
  character-set-server=utf8
  init_connect='SET NAMES utf8
  port        = 3306
  socket      = /tmp/mysql.sock
  skip-external-locking
  key_buffer_size = 16M
  max_allowed_packet = 1M
table_open_cache = 64
  sort_buffer_size = 512K
  net_buffer_length = 8K
  read_buffer_size = 256K
  read_rnd_buffer_size = 512K
  myisam_sort_buffer_size = 8M
  character-set-server=utf8
  init_connect='SET NAMES utf8'
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
 
  # Replication Master Server (default)
  # binary logging is required for replication
  log-bin=mysql-bin
# binary logging format - mixed recommended
    binlog_format=mixed
 
      # required unique id between 1 and 2^32 - 1
      # defaults to 1 if master-host is not set
      # but will not function as a master if omitted
      server-id   = 1
 
    # Replication Slave (comment out master section to use this)
    #
    # To configure this host as a replication slave, you can choose between
    # two methods :
    #
    # 1) Use the CHANGE MASTER TO command (fully described in our manual) -
    #    the syntax is:
    #
    #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
    #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
    #
    #    where you replace <host>, <user>, <password> by quoted strings and
    #    <port> by the master's port number (3306 by default).
    #
    #    Example:
    #
    #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
    #    MASTER_USER='joe', MASTER_PASSWORD='secret';
    #
    # OR
#
    # 2) Set the variables below. However, in case you choose this method, then  
    #    start replication for the first time (even unsuccessfully, for example  
    #    if you mistyped the password in master-password and the slave fails to  
    #    connect), the slave will create a master.info file, and any later
    #    change in this file to the variables' values below will be ignored and  
    #    overridden by the content of the master.info file, unless you shutdown  
    #    the slave server, delete master.info and restart the slaver server.
    #    For that reason, you may want to leave the lines below untouched
    #    (commented) and instead use CHANGE MASTER TO (see above)
    #
    # required unique id between 2 and 2^32 - 1
    # (and different from the master)
    # defaults to 2 if master-host is set
    # but will not function as a slave if omitted
    #server-id       = 2
    #
    # The replication master for this slave - required
    #master-host     =   <hostname>
    #
    # The username the slave will use for authentication when connecting
    # to the master - required
    #master-user     =   <username>
        [mysqldump]
        quick
        max_allowed_packet = 16M
 
          [mysql]
          no-auto-rehash
          # Remove the next comment character if you are not familiar with SQL
          #safe-updates
          default-character-set=utf8
 
        [myisamchk]
        key_buffer_size = 20M
        sort_buffer_size = 20M
        read_buffer = 2M
        write_buffer = 2M
 
          [mysqlhotcopy]
          interactive-timeout

在设置里找到的mysql,点击configuration,在configuration中选择/private/etc/my.cnf,然后重启mysql。Macbook安装mysql+php+phpmyadming进行网站构建笔记

2、php连接数据库问题,在/Library/WebServer/Documents文件夹下新建index.php文件编程测试连接mysql情况,代码如何如下:

<?php
    $con = mysqli_connect("localhost","root","qwertyuiop","test");
    //测试是否连接数据库
    if($con){
      echo "连接成功";
    }
    else{
      echo "连接失败 ";
    }
?>

通过火狐输入localhost/index.php结果报错mysqli_connect连接有问题

在网上查了一下,说是本地socket设置与默认的不一样,导致php无法找到mysql的socket文件。根据网上提供的方法,需要做如下操作。

首先,在MySQL中用status查看数据库状态,如下所示:

Macbook安装mysql+php+phpmyadming进行网站构建笔记

其中,标红的部分是我们需要的。然后打开php.ini文件,需要将mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket的值设置为标红后面的那个目录。重启apache服务器,后续info.php工作正常,MySQL的版本信息能够正常显示了。(php.ini不找不到,把/private/ect/php.ini.default 文件复制后,去掉.default即可)。

3、phpmyadmin设置问题,通过火狐输入:localhost/phpmyadmin/进入phpmyadmin登录界面,直接输入用户名密码,结果报错

Macbook安装mysql+php+phpmyadming进行网站构建笔记

mysqli_real_connect(): The server requested authentication method unknown to the client

是由于新版本的mysql账号密码解锁机制不一致导致的

解决办法:

打开终端,输入mysql -uroot -p,在输入密码,进入mysql操作界面。

输入use mysql;

再输入 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

即可。

好了!大事搞定!可以测试php连接数据库了!心塞了好几天!!