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。

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查看數據庫狀態,如下所示:

其中,標紅的部分是我們需要的。然後打開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登錄界面,直接輸入用戶名密碼,結果報錯

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連接數據庫了!心塞了好幾天!!


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章