MySQL免編譯安裝及登入(5.6.36)

初雪之路發表於2018-11-25

一、下載MySQL 5.6.36免編譯安裝包並上傳至 /usr/local/src 目錄(也可以使用wget命令直接下載至該目錄)

下載地址 https://yunpan.360.cn/surl_ymCGfFYt2Ya

二、解壓縮

[root@JSH-01 src]# tar zxvf  mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

三、將解壓縮後的檔案移動至 /usr/local/ 目錄下並改名為mysql

[root@JSH-01 src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
[root@JSH-01 local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src

四、進入mysql目錄

[root@JSH-01 local]# cd mysql
[root@JSH-01 mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

五、建立mysql使用者和SQL資料庫存放目錄

[root@JSH-01 mysql]# useradd mysql
[root@JSH-01 mysql]# mkdir /data

六、初始化安裝MySQL並制定使用者和目錄,安裝前需要先安裝相關的依賴包。

[root@JSH-01 mysql]# yum install -y perl gcc kernel-devel
[root@JSH-01 mysql]# yum install -y autoconf
[root@JSH-01 mysql]# yum install -y libaio
[root@JSH-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

執行完畢後輸入命令 echo$?  回車看輸出的結果是0還是1,如果是0則表示安裝成功。或者看到兩次 OK 也表示安裝成功。

[root@JSH-01 mysql]# echo $?
0

七、拷貝配置檔案和啟動指令碼

[root@JSH-01 mysql]# cp support-files/my-default.cnf /etc/my.cnf(拷貝配置檔案並改名為 my.cnf )
cp: overwrite ‘/etc/my.cnf’? y
[root@JSH-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld(拷貝啟動指令碼並改名為 mysqld)
[root@JSH-01 mysql]# vi /etc/init.d/mysqld

拷貝完成之後編輯啟動指令碼mysqld並定義basedir 和 datadir
basedir=/usr/local/mysql
datadir=/data/mysql

八、儲存完畢啟動指令碼後將其許可權更改為 755(實際此檔案預設許可權就是 755)

[root@JSH-01 mysql]# ls -l /etc/init.d/mysqld  檢視檔案詳細資訊
-rwxr-xr-x 1 root root 10592 Nov 25 20:08 /etc/init.d/mysqld

九、設定MySQL開機啟動

[root@JSH-01 mysql]# chkconfig --add mysqld
[root@JSH-01 mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use `systemctl list-unit-files`.
      To see services enabled on particular target use
      `systemctl list-dependencies [target]`.

aegis              0:off    1:off    2:on    3:on    4:on    5:on    6:off
agentwatch         0:off    1:off    2:on    3:on    4:on    5:on    6:off
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off

十、啟動MySQL服務

[root@JSH-01 mysql]# service mysqld start
Starting MySQL.Logging to `/data/mysql/JSH-01.err`.
                                                           [  OK  ]
[root@JSH-01 mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3234/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      22293/mysqld 
由此可見MySQL使用的服務埠為3306

十一、如何停止MySQL服務

可以使用命令  killall mysqld 或者 service mysqld stop

十二、安裝完之後如何登入MySQL ?

[root@JSH-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
提示未找到是因為mysql命令路徑為:/usr/local/mysql/bin/ 此時需要更改環境變數。編輯配置檔案 vi  /etc/profile 將export PATH=$PATH:/usr/local/mysql/bin/寫入最後一行。

[root@JSH-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@JSH-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@JSH-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> quit;
Bye
[root@JSH-01 bin]# vi /etc/profile
[root@JSH-01 bin]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>


相關文章