美文网首页
mac下安装MySQL和Redis

mac下安装MySQL和Redis

作者: _零 | 来源:发表于2018-09-29 13:56 被阅读0次

    Redis安装使用

    通过homebrew安装

    brew install redis
    

    安装完之后会显示如下信息

    To have launchd start redis now and restart at login:
      brew services start redis
    Or, if you don't want/need a background service you can just run:
      redis-server /usr/local/etc/redis.conf
    

    启动redis服务
    上述两种情况是如何启动redis的,第一种后台运行

    brew services start redis
    

    第二种情况即非后台运行

    redis-server /usr/local/etc/redis.conf
    

    使用第二种方式,显示如下结果说明redis服务器安装成功
    注:启动redis服务器之后终端所在的窗口就不能输入别的命令了,需要在终端打开新的窗口才能使用客户端功能

    5417:C 01 Jun 12:45:37.066 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    5417:C 01 Jun 12:45:37.067 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=5417, just started
    5417:C 01 Jun 12:45:37.067 # Configuration loaded
    5417:M 01 Jun 12:45:37.068 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                    _._
               _.-``__ ''-._
          _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 5417
      `-._    `-._  `-./  _.-'    _.-'
     |`-._`-._    `-.__.-'    _.-'_.-'|
     |    `-._`-._        _.-'_.-'    |           http://redis.io
      `-._    `-._`-.__.-'_.-'    _.-'
     |`-._`-._    `-.__.-'    _.-'_.-'|
     |    `-._`-._        _.-'_.-'    |
      `-._    `-._`-.__.-'_.-'    _.-'
          `-._    `-.__.-'    _.-'
              `-._        _.-'
                  `-.__.-'
     
    5417:M 01 Jun 12:45:37.070 # Server initialized
    5417:M 01 Jun 12:45:37.070 * DB loaded from disk: 0.000 seconds
    5417:M 01 Jun 12:45:37.070 * Ready to accept connections
    

    使用如下命令可以使redis后台运行

    nohup redis-server /usr/local/etc/redis.conf &
    

    查看redis服务是否启动

    ps aux | grep redis
    

    通过redis-cli命令可以启动redis客户端

    redis-cli
    

    接下来就可以操作redis了
    示例:

    127.0.0.1:6379> keys *
    (empty list or set)
    127.0.0.1:6379> get 123
    (nil)
    127.0.0.1:6379> set 123 jim
    OK
    127.0.0.1:6379> get 123
    "jim"
    127.0.0.1:6379> keys *
    1) "123"
    127.0.0.1:6379> set a 123
    OK
    127.0.0.1:6379> EXPIRE a 3600
    (integer) 1
    127.0.0.1:6379> TTL a
    (integer) 3594
    127.0.0.1:6379> TTL a
    (integer) 3591
    

    退出redis服务
    (1)客户端退出

    redis-cli shutdown
    

    2)关闭pid

    ps -u XXX(替换成你的用户名) -o pid,rss,command | grep redis-server
    

    查看结果

    5888   3432 /usr/local/opt/redis/bin/redis-server 127.0.0.1:6379
    5919    232 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis-server
    

    关闭pid

    kill 5888
    

    ps:如果你的电脑里面安装了oh my zsh,只需要在终端里输入

    kill redis
    

    按下tab,会自动替换成对应的pid

    安装Redis扩展

    查看php有哪些扩展

    php -m
    

    下载phpredis

    git clone https://github.com/nicolasff/phpredis
    

    进入phpredis目录下

    cd phpredis
    

    运行phpize命令生成配置文件

    sudo phpize 
    

    这时如果没有安装m4和autoconf就会出现下面错误

    Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script
    

    安装m4和autoconf

    brew install m4
    brew install autoconf
    

    安装完之后再回到phpredis目录下执行phpize,显示如下结果则成功

    Configuring for:
    PHP Api Version:         20131106
    Zend Module Api No:      20131226
    Zend Extension Api No:   220131226
    

    运行./configure:
    需要找一下php-config的位置:

    find / -name php-config
    

    我本地的位置是:/usr/bin/php-config,所以我执行的命令是:

    ./configure --with-php-config=/usr/bin/php-config
    

    编译安装

    make && make install
    

    这时程序会讲编译好的.so 文件拷贝到php默认的扩展文件夹下面,但是mac的 /usr 文件下的有些目录 root 账户也没有写权限,所以会报错。这时 需要 重启电脑 按住 command + r 进入安全模式,打开终端 csrutil disable 将 csrutil 这个功能禁用掉,这样root就有权限了,重新执行命令,得到如下结果:

    Libraries have been installed in:
       /usr/local/redis-3.1.2/modules ........
    Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20131226/ 
    

    配置php.ini
    我的php.ini文件是在 /etc下,如果没有该文件 则将php.ini.default文件copy一下
    在php.ini文件中添加

    extension = redis.so
    

    重启nginx
    查看phpinfo()
    输入如下命令可以看到redis

    php -m|grep redis
    

    升级系统之后导致的问题
    最近升级了一下系统,出现了好多问题。

    找到了解决方法
    https://www.jianshu.com/p/d34d99001aab

    但是我的redis扩展没有了,没办法我只能重装。
    重复上述步骤

    phpize
    

    但是报了如下错误

    grep: /usr/include/php/main/php.h: No such file or directory
    grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
    grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
    

    上网查了一下方法,找到了解决方法

    https://mengkang.net/755.html

    就是我们本来有这些文件,但是都换目录了
    那么我们就找一下这些东西在哪个目录

    sudo find /-name php.h
    

    找到了在这个目录下

    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
    

    然后我们创建个软链接

    sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /usr/include
    

    然后再依次执行上述步骤就可以了

    ./configure --with-php-config=/usr/bin/php-config
    make
    make test
    sudo make install
    

    Mysql安装使用

    安装

    brew install mysql
    

    在命令行运行mysql,出现以下错误

    
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    

    原因,mysql没有启动,解决方法

    mysql.server start
    

    结果如下,成功启动

    Starting MySQL
    . SUCCESS!
    

    接下来便可以运行mysql了,
    下面是因为安装mysql默认的用户名是root,端口是3306,导致的权限问题,用root登就可以了

    mysql
    ERROR 1045 (28000): Access denied for user '************' (using password: NO)
    a088@088deMacBook-Pro:~|⇒  sudo mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6
    Server version: 5.7.22 Homebrew
     
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
     
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
     
    mysql>
    

    连接远程服务器数据库

    mysql -h host -P PORT -u user -p password
    

    相关文章

      网友评论

          本文标题:mac下安装MySQL和Redis

          本文链接:https://www.haomeiwen.com/subject/gbrjoftx.html