MacOS下配置mysql server 和环境变量实现命令行调用的教程

博主是在校大二学生,粗浅的把自己在mac上摸索出的经验整理一下

这两天刚把学校布置在oracle平台上的sql命令实验做完,开始着手做数据库课程设计

众所周知,学校的老师一般都会认为你们用的是windows系统(此处省略一万句mmp),所以macos上安装软件都必须自己摸索

首先要建立数据库,我选择了先下载mysql 搭环境 

百度搜索 mysql 第一项就能找到

进入mysql的官网,找到download界面

我们要下载的是mysql server 的commuity版本

选择图中出现的版本下载即可

当然可能版本没有很重要,博主选择的是macOS 10.14 (x86, 64-bit), DMG Archive版本 当前最新的版本号是8.0.13

如上图,如果是Windows系统的小伙伴,可能就不太一样了

下载下来文件以后,点开.dmg文件运行 会出现一个.pkg文件 点开运行即可

接下来是本文的重点所在 我们可以点开苹果的finder 看一下我们的mysql文件下载在哪里了

在我们的Macintosh HD下的/usr/local目录下有两个文件 一个是mysql 一个是mysql-8.0.13-macos10.14-x86-64

比对了一下其实这两个文件在我看来是存储了相同的内容,也不敢删除哪一个,万一不好用了就比较麻烦

可以对比一下,可以发现你们电脑里的mysql文件夹下没有my.cnf 

这是mac下mysql的配置文件 Windows下应该是叫my.ini 

为什么要添加这个文件呢?

因为博主在终端想要调mysql命令的时候,比如在主用户目录下输入$mysql -u root -p的时候

总是给我报command not found 的错误

然后一个简单的方法是在命令行里创建一个一次性链接

输入命令:

$ln -s /usr/local/mysql/bin/mysql /usr/bin

一般来说这时候再去输入$mysql -u root -p 就会跳密码输入提示了

但是博主脸比较黑   命令行提示我:

ln: /usr/bin/mysql: Operation not permitted

那没事 ,我们用sudo提高一下权限 输入命令:

$sudo ln -s /usr/local/mysql/bin/mysql /usr/bin

然后输入密码 发现还是不行 接着给我 ln: /usr/bin/mysql: Operation not permitted

那么没办法 只好采用权宜之计 先用alias命令改个名用一下吧 输入命令:

$alias mysql=/usr/local/mysql/bin/mysql 

这时候不管脸再怎么黑,都应该可以输入mysql命令了 可以先用这个方法测试一下

但是难道我每次登陆都去输入一个这个命令吗? 

百度搜索了一下,发现好多博主都给出了这样的解答

 

 

输入:

cd  /usr/local/mysql

回车执行

然后输入:

sudo vim .bash_profile

回车执行

需要输入root用户密码。sudo是使用root用户修改环境变量文件。

进入编辑器后,我们先按"i”,即切换到“插入”状态。

在文档的最下方输入:

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

然后按Esc退出insert状态,  

:wq

实现保存

however 我改过之后也还是不行 可能有些小伙伴可以用这个方法实现

我选择了自己再加一个配置文件

1.在 /etc 新建 my.cnf 文件

sudo vim my.cnf

2.同样的方法,将如下配置内容写入到文件中

# Example MySQL config file for medium systems.  
  #  
  # This is for a system with little memory (32M - 64M) where MySQL plays  
  # an important part, or systems up to 128M where MySQL is used together with  
  # other programs (such as a web server)  
  #  
  # MySQL programs look for option files in a set of  
  # locations which depend on the deployment platform.  
  # You can copy this option file to one of those  
  # locations. For information about these locations, see:  
  # http://dev.mysql.com/doc/mysql/en/option-files.html  
  #  
  # In this file, you can use all long options that a program supports.  
  # If you want to know which options a program supports, run the program  
  # with the "--help" option.  
  # The following options will be passed to all MySQL clients  
  [client]
  default-character-set=utf8
  #password   = your_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>  
    #  
    # The password the slave will authenticate with when connecting to  
    # the master - required  
    #master-password =   <password>  
    #  
    # The port the master is listening on.  
    # optional - defaults to 3306  
    #master-port     =  <port>  
    #  
    # binary logging - not required for slaves, but recommended  
    #log-bin=mysql-bin  

      # Uncomment the following if you are using InnoDB tables  
      #innodb_data_home_dir = /usr/local/mysql/data  
      #innodb_data_file_path = ibdata1:10M:autoextend  
      #innodb_log_group_home_dir = /usr/local/mysql/data  
      # You can set .._buffer_pool_size up to 50 - 80 %  
      # of RAM but beware of setting memory usage too high  
      #innodb_buffer_pool_size = 16M  
      #innodb_additional_mem_pool_size = 2M  
      # Set .._log_file_size to 25 % of buffer pool size  
      #innodb_log_file_size = 5M  
      #innodb_log_buffer_size = 8M  
      #innodb_flush_log_at_trx_commit = 1  
      #innodb_lock_wait_timeout = 50  

        [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

在这之后,你需要把出现在主用户目录下的my.cnf移到mysql目录下 ,实现正常命令执行

出现欢迎界面,大功告成

此后输入mysql -u root -p 不再会报command not found

希望大家数据库课设做的愉快