Linux環境下安裝MySQL數據庫

  Linux安裝mysql服務分兩種安裝方法:html

  (1)、源碼安裝,優勢是安裝包比較小,只有十多M,缺點是安裝依賴的庫多,安裝編譯時間長,安裝步驟複雜容易出錯;mysql

  (2)、使用官方編譯好的二進制文件安裝,優勢是安裝速度快,安裝步驟簡單,缺點是安裝包很大,300M左右。如下介紹linux使用官方編譯好的二進制包安裝mysql。linux

  樓主用的是第二種方法,首先說一下環境參數:sql

    硬件配置:
      CPU: Xeon(R) CPU E5-2650 v4 @ 2.20GHz 8核
      內存:16G
      硬盤:系統盤200GB 數據盤1TB
    操做系統
      CentOS 7.2 64位
    數據庫
      MySQL 5.6.43shell

   具體步驟以下:數據庫

  一、下載MySQL數據包,地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads,注意選擇相應的數據包,樓主下載的是5.6.43通用版,Linux下64位vim

  

  二、檢查當前環境下是否已安裝MySQL服務socket

  輸入命令:ui

find / -name mysql

  若是已安裝會顯示以下:spa

 

 

  若是要刪除老版本或者之前的殘餘文件,可執行: find / -name mysql|xargs rm -rf,或者rm -rf 上邊查找到的路徑,多個路徑用空格隔開。

 

  三、處理MySQL文件

  將下載的文件上傳到Linux的/data下並解壓,固然了,也能夠放置在你想放置的目錄下。

tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

  將解壓後的文件重命名爲mysql

mv mysql-5.6.43-linux-glibc2.12-x86_64 mysql

  四、建立mysql用戶組及用戶

groupadd mysql useradd -r -g mysql mysql

  五、進入到mysql目錄,執行添加MySQL配置的操做

cp support-files/my-medium.cnf /etc/my.cnf 或: cp support-files/my-default.cnf /etc/my.cnf

  是否覆蓋?按y 回車

  六、編輯/etc/my.cnf文件

vim /etc/my.cnf

  進入編輯模式,添加或者修改相關配置,更改完成後保存退出

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = /data/mysql datadir = /data/mysql/data port = 3306 # server_id = ..... socket = /tmp/mysql.sock character-set-server = utf8 skip-name-resolve log-err = /data/mysql/data/error.log pid-file = /data/mysql/data/mysql.pid # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

  七、在mysql當前目錄下設定目錄的訪問權限(注意後面的小點,表示當前目錄)

chown -R mysql . chgrp -R mysql . scripts/mysql_install_db --user=mysql chown -R root . chown -R mysql data

  八、初始化數據(在mysql/bin或者mysql/scripts下有個 mysql_install_db 可執行文件初始化數據庫),進入mysql/bin或者mysql/scripts目錄下,執行下面命令

./mysql_install_db --verbose --user=root --defaults-file=/etc/my.cnf --datadir=/data/mysql/data --basedir=/data/mysql --pid-file=/data/mysql/data/mysql.pid --tmpdir=/tmp

  九、啓動mysql,進入/usr/local/mysql/bin目錄,執行下面命令,若是沒有這個目錄不執行也無所謂

./mysqld_safe --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root &

  十、設置開機啓動,新開啓shell中斷後,進入mysql目錄,執行下面命令

cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld cp /data/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql chmod 700 /etc/init.d/mysql chkconfig --add mysqld chkconfig --level 2345 mysqld on chown mysql:mysql -R /data/mysql/

  重啓linux

reboot

  查看mysql狀態

service mysqld status

  十一、添加遠程訪問權限

  (1)、添加mysql命令

ln  -s /data/mysql/bin/mysql  /usr/bin  (mysql的安裝路徑)

  (2)、更改訪問權限

#登陸mysql,執行下面命令 mysql -uroot -p #密碼爲空直接回車,運行如下兩條命令 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' with grant option; Flush privileges;

  退出mysql,重啓linux

exit reboot

 

  注:本機訪問mysql,root帳戶默認是沒有密碼的,端口號默認3306,若是須要修改root帳戶密碼,在/data/mysql/bin目錄下,執行下面命令

./mysqladmin -h 127.0.0.1 -P3306 -uroot password 'root'exit

   密碼已經修改成root,能夠登陸使用了。

相關文章
相關標籤/搜索