1. 程式人生 > >Mac OS安裝mysql教程

Mac OS安裝mysql教程

前兩天為了開發方便,在自己電腦上裝了一個mysql,發現Mac上的安裝步驟中還是有些注意事項的,於是整理了這篇部落格。

1、下載

進入官網www.mysql.com下載所需的mysql版本和安裝包格式。  注:Mac需下載dmg格式檔案。

這裡寫圖片描述

下載完畢後,點選pkg安裝包,next進行安裝。

2、安裝

安裝完畢後,進入系統偏好設定,找到mysql。 這裡寫圖片描述

點選Start Mysql Server,顯示啟動成功。

Tips:  (1)mysql預設安裝路徑是:/usr/local/mysql  (2)此時需要記錄下臨時密碼,如w?8gUt-xfp_s。

3、配置

3.1修改臨時密碼

資料庫連線工具無法使用臨時密碼進行連線,會報錯密碼失效。

登入mysql的預設路徑的bin下,使用如下命令,進行修改。其中,123456為你的新密碼。

這裡寫圖片描述  

3.2修改字符集

一般,my-default.cnf檔案在mysql/support-files下。若存在,將其拷貝到/etc下,並修改名稱為my.cnf。若沒有,則手動建立該檔案於/etc下。

新建檔案命令: sudo vim my.cnf

設定檔案許可權: chmod 775 my.cnf

新增檔案內容:  將如下配置內容寫入到檔案中

<span style="color:#000000"><code># Example MySQL config <span style="color:#000088">file</span> <span style="color:#000088">for</span> medium systems.  
  #  
  # This <span style="color:#000088">is</span> <span style="color:#000088">for</span> a system <span style="color:#000088">with</span> little memory (<span style="color:#006666">32</span>M - <span style="color:#006666">64</span>M) where MySQL plays  
  # an important part, <span style="color:#000088">or</span> systems up <span style="color:#000088">to</span> <span style="color:#006666">128</span>M where MySQL <span style="color:#000088">is</span> used together <span style="color:#000088">with</span>  
  # other programs (such as a web server)  
  #  
  # MySQL programs look <span style="color:#000088">for</span> option files <span style="color:#000088">in</span> a set <span style="color:#000088">of</span>  
  # locations which depend <span style="color:#000088">on</span> the deployment platform.  
  # You can copy this option <span style="color:#000088">file</span> <span style="color:#000088">to</span> one <span style="color:#000088">of</span> those  
  # locations. <span style="color:#000088">For</span> information about these locations, see:  
  # http://dev.mysql.com/doc/mysql/en/option-files.html  
  #  
  # <span style="color:#000088">In</span> this <span style="color:#000088">file</span>, you can <span style="color:#000088">use</span> <span style="color:#000088">all</span> long options that a program supports.  
  # <span style="color:#000088">If</span> you want <span style="color:#000088">to</span> know which options a program supports, run the program  
  # <span style="color:#000088">with</span> the <span style="color:#009900">"--help"</span> option.  
  # The following options will be passed <span style="color:#000088">to</span> <span style="color:#000088">all</span> MySQL clients  
  [client]
  <span style="color:#000088">default</span>-<span style="color:#4f4f4f">character</span>-set=utf8
  #password   = your_password  
  <span style="color:#000088">port</span>        = <span style="color:#006666">3306</span>  
  socket      = /tmp/mysql.sock   
  # Here follows entries <span style="color:#000088">for</span> some specific programs  
  # The MySQL server  
  [mysqld]
  <span style="color:#4f4f4f">character</span>-set-server=utf8
  init_connect='SET  NAMES utf8'
  <span style="color:#000088">port</span>        = <span style="color:#006666">3306</span>  
  socket      = /tmp/mysql.sock  
  skip-external-locking  
  key_buffer_size = <span style="color:#006666">16</span>M  
  max_allowed_packet = <span style="color:#006666">1</span>M  
  table_open_cache = <span style="color:#006666">64</span>  
  sort_buffer_size = <span style="color:#006666">512</span>K  
  net_buffer_length = <span style="color:#006666">8</span>K  
  read_buffer_size = <span style="color:#006666">256</span>K  
  read_rnd_buffer_size = <span style="color:#006666">512</span>K  
  myisam_sort_buffer_size = <span style="color:#006666">8</span>M  
  <span style="color:#4f4f4f">character</span>-set-server=utf8  
  init_connect='SET  NAMES utf8' 
# Don't listen <span style="color:#000088">on</span> a TCP/IP <span style="color:#000088">port</span> at <span style="color:#000088">all</span>. This can be a security enhancement,  
# <span style="color:#000088">if</span> <span style="color:#000088">all</span> processes that need <span style="color:#000088">to</span> connect <span style="color:#000088">to</span> mysqld run <span style="color:#000088">on</span> the same host.  
# <span style="color:#000088">All</span> interaction <span style="color:#000088">with</span> mysqld must be made via Unix sockets <span style="color:#000088">or</span> named pipes.  
# Note that using this option without enabling named pipes <span style="color:#000088">on</span> Windows  
# (via the <span style="color:#009900">"enable-named-pipe"</span> option) will render mysqld useless!  
#   
#skip-networking  
# Replication Master Server (<span style="color:#000088">default</span>)  
  # binary logging <span style="color:#000088">is</span> required <span style="color:#000088">for</span> replication  
  log-bin=mysql-bin  
# binary logging format - mixed recommended  
    binlog_format=mixed  
# required unique id between <span style="color:#006666">1</span> <span style="color:#000088">and</span> <span style="color:#006666">2</span>^<span style="color:#006666">32</span> - <span style="color:#006666">1</span>  
      # defaults <span style="color:#000088">to</span> <span style="color:#006666">1</span> <span style="color:#000088">if</span> master-host <span style="color:#000088">is</span> <span style="color:#000088">not</span> set  
      # but will <span style="color:#000088">not</span> <span style="color:#000088">function</span> as a master <span style="color:#000088">if</span> omitted  
      server-id   = <span style="color:#006666">1</span>  
# Replication Slave (comment <span style="color:#000088">out</span> master section <span style="color:#000088">to</span> <span style="color:#000088">use</span> this)  
    #  
    # <span style="color:#000088">To</span> configure this host as a replication slave, you can choose between  
    # two methods :  
    #  
    # <span style="color:#006666">1</span>) <span style="color:#000088">Use</span> the CHANGE MASTER <span style="color:#000088">TO</span> command (fully described <span style="color:#000088">in</span> our manual) -  
    #    the syntax <span style="color:#000088">is</span>:  
    #  
    #    CHANGE MASTER <span style="color:#000088">TO</span> MASTER_HOST=<host>, MASTER_PORT=<<span style="color:#000088">port</span>>,  
    #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;  
    #  
    #    where you replace <host>, <user>, <password> by quoted strings <span style="color:#000088">and</span>  
    #    <<span style="color:#000088">port</span>> by the master's <span style="color:#000088">port</span> number (<span style="color:#006666">3306</span> by <span style="color:#000088">default</span>).  
    #  
    #    Example:  
    #  
    #    CHANGE MASTER <span style="color:#000088">TO</span> MASTER_HOST='<span style="color:#006666">125.564</span><span style="color:#006666">.12</span><span style="color:#006666">.1</span>', MASTER_PORT=<span style="color:#006666">3306</span>,  
    #    MASTER_USER='joe', MASTER_PASSWORD='secret';  
    #  
    # <span style="color:#000088">OR</span>  
    #  
    # <span style="color:#006666">2</span>) Set the variables below. However, <span style="color:#000088">in</span> <span style="color:#000088">case</span> you choose this method, <span style="color:#000088">then</span>  
    #    start replication <span style="color:#000088">for</span> the first <span style="color:#4f4f4f">time</span> (even unsuccessfully, <span style="color:#000088">for</span> example  
    #    <span style="color:#000088">if</span> you mistyped the password <span style="color:#000088">in</span> master-password <span style="color:#000088">and</span> the slave fails <span style="color:#000088">to</span>  
    #    connect), the slave will create a master.info <span style="color:#000088">file</span>, <span style="color:#000088">and</span> any later  
    #    change <span style="color:#000088">in</span> this <span style="color:#000088">file</span> <span style="color:#000088">to</span> the variables' values below will be ignored <span style="color:#000088">and</span>  
    #    overridden by the content <span style="color:#000088">of</span> the master.info <span style="color:#000088">file</span>, unless you shutdown  
    #    the slave server, delete master.info <span style="color:#000088">and</span> restart the slaver server.  
    #    <span style="color:#000088">For</span> that reason, you may want <span style="color:#000088">to</span> leave the lines below untouched  
    #    (commented) <span style="color:#000088">and</span> instead <span style="color:#000088">use</span> CHANGE MASTER <span style="color:#000088">TO</span> (see above)  
    #  
    # required unique id between <span style="color:#006666">2</span> <span style="color:#000088">and</span> <span style="color:#006666">2</span>^<span style="color:#006666">32</span> - <span style="color:#006666">1</span>  
    # (<span style="color:#000088">and</span> different from the master)  
    # defaults <span style="color:#000088">to</span> <span style="color:#006666">2</span> <span style="color:#000088">if</span> master-host <span style="color:#000088">is</span> set  
    # but will <span style="color:#000088">not</span> <span style="color:#000088">function</span> as a slave <span style="color:#000088">if</span> omitted  
    #server-id       = <span style="color:#006666">2</span>  
    #  
    # The replication master <span style="color:#000088">for</span> this slave - required  
    #master-host     =   <hostname>  
    #  
    # The username the slave will <span style="color:#000088">use</span> <span style="color:#000088">for</span> authentication <span style="color:#000088">when</span> connecting  
    # <span style="color:#000088">to</span> the master - required  
    #master-user     =   <username>  
    #  
    # The password the slave will authenticate <span style="color:#000088">with</span> <span style="color:#000088">when</span> connecting <span style="color:#000088">to</span>  
    # the master - required  
    #master-password =   <password>  
    #  
    # The <span style="color:#000088">port</span> the master <span style="color:#000088">is</span> listening <span style="color:#000088">on</span>.  
    # optional - defaults <span style="color:#000088">to</span> <span style="color:#006666">3306</span>  
    #master-<span style="color:#000088">port</span>     =  <<span style="color:#000088">port</span>>  
    #  
    # binary logging - <span style="color:#000088">not</span> required <span style="color:#000088">for</span> slaves, but recommended  
    #log-bin=mysql-bin  
# Uncomment the following <span style="color:#000088">if</span> you are using InnoDB tables  
      #innodb_data_home_dir = /usr/local/mysql/data  
      #innodb_data_file_path = ibdata1:<span style="color:#006666">10</span>M:autoextend  
      #innodb_log_group_home_dir = /usr/local/mysql/data  
      # You can set .._buffer_pool_size up <span style="color:#000088">to</span> <span style="color:#006666">50</span> - <span style="color:#006666">80</span> %  
      # <span style="color:#000088">of</span> RAM but beware <span style="color:#000088">of</span> setting memory usage too high  
      #innodb_buffer_pool_size = <span style="color:#006666">16</span>M  
      #innodb_additional_mem_pool_size = <span style="color:#006666">2</span>M  
      # Set .._log_file_size <span style="color:#000088">to</span> <span style="color:#006666">25</span> % <span style="color:#000088">of</span> <span style="color:#000088">buffer</span> pool size  
      #innodb_log_file_size = <span style="color:#006666">5</span>M  
      #innodb_log_buffer_size = <span style="color:#006666">8</span>M  
      #innodb_flush_log_at_trx_commit = <span style="color:#006666">1</span>  
      #innodb_lock_wait_timeout = <span style="color:#006666">50</span>  
[mysqldump]  
        quick  
        max_allowed_packet = <span style="color:#006666">16</span>M  
[mysql]  
          no-auto-rehash  
          # Remove the <span style="color:#000088">next</span> comment <span style="color:#4f4f4f">character</span> <span style="color:#000088">if</span> you are <span style="color:#000088">not</span> familiar <span style="color:#000088">with</span> SQL  
          #safe-updates  
          <span style="color:#000088">default</span>-<span style="color:#4f4f4f">character</span>-set=utf8   
[myisamchk]  
        key_buffer_size = <span style="color:#006666">20</span>M  
        sort_buffer_size = <span style="color:#006666">20</span>M  
        read_buffer = <span style="color:#006666">2</span>M  
        write_buffer = <span style="color:#006666">2</span>M  
[mysqlhotcopy]  
          interactive-timeout
</code></span>

重啟後登入mysql client ,輸入show variables like ‘%char%’;進行檢視。

4、小結

選擇穩定的且適合你係統的安裝包;  許可權問題實在搞不定記得sudo;  vi操作檔案要小心。

以上,世上無難事,只怕有心人。