天天看點

linux 伺服器安裝MySQL 全攻略一丶檢視伺服器系統版本資訊二丶去Mysql 官網下載下傳安裝包三丶導入你的伺服器(如果你也用Xshell6的話,我覺得還蠻友善的 。。。)四丶以RPM方式安裝MySQL五丶mysql安裝完成後,檢視mysql的安裝路徑,啟動、停止mysql服務六丶修改mysql的安裝路徑七丶啟動服務,登入mysql(這裡有坑,同學們注意看哦)這裡要注意了,可能有個坑:

最近自己掌握一個項目,是以搭建環境的事兒就不可避免,最最基礎的莫過于資料庫,是以寫個全攻略,為大家解決共同的坑!

一丶檢視伺服器系統版本資訊

指令:

cat /etc/issue

這裡我的伺服器不能給大家看(保密條例啊,求原諒)

總之,我的機器6U 這邊安裝版本号也需要是6U的

二丶去Mysql 官網下載下傳安裝包

官網位址:

https://dev.mysql.com/downloads/mysql/5.6.html#downloads

我要安裝的是5.6.43,以這個為例進行安裝了啊

linux 伺服器安裝MySQL 全攻略一丶檢視伺服器系統版本資訊二丶去Mysql 官網下載下傳安裝包三丶導入你的伺服器(如果你也用Xshell6的話,我覺得還蠻友善的 。。。)四丶以RPM方式安裝MySQL五丶mysql安裝完成後,檢視mysql的安裝路徑,啟動、停止mysql服務六丶修改mysql的安裝路徑七丶啟動服務,登入mysql(這裡有坑,同學們注意看哦)這裡要注意了,可能有個坑:

三丶導入你的伺服器(如果你也用Xshell6的話,我覺得還蠻友善的 。。。)

懶人專用:請依次粘貼執行~

指令:mkdir mysql-install-package  

指令:cp MySQL-5.6.43-1.el6.x86_64.rpm-bundle.tar  mysql-install-package/

指令:cd mysql-install-package  

指令:ls

顯示:MySQL-5.6.43-1.el6.x86_64.rpm-bundle.tar

指令:tar -xvf MySQL-5.6.43-1.el6.x86_64.rpm-bundle.tar

顯示:

MySQL-shared-5.6.43-1.el6.x86_64.rpm

MySQL-devel-5.6.43-1.el6.x86_64.rpm

MySQL-client-5.6.43-1.el6.x86_64.rpm

MySQL-test-5.6.43-1.el6.x86_64.rpm

MySQL-server-5.6.43-1.el6.x86_64.rpm

MySQL-embedded-5.6.43-1.el6.x86_64.rpm

MySQL-shared-compat-5.6.43-1.el6.x86_64.rpm

四丶以RPM方式安裝MySQL

1.在RHEL系統中安裝mysql需要先運作MySQL-shared-5.6.43-1.el6.x86_64.rpm,這個相容包,然後才能安裝server,先後執行以下指令進行mysql的安裝(權限不夠,sudo來湊)

依次執行,過程很精彩,請認真檢視(你要是不嫌累的話,手動皮一下~)

1.yum install MySQL-shared-5.6.43-1.el6.x86_64.rpm   #安裝mysql相容包

2.yum install MySQL-server-5.6.43-1.el6.x86_64.rpm    #安裝mysql的伺服器程式

3.yum install MySQL-client-5.6.43-1.el6.x86_64.rpm     #安裝mysql的用戶端程式

4.yum install MySQL-devel-5.6.43-1.el6.x86_64.rpm    #安裝mysql的庫和頭檔案

五丶mysql安裝完成後,檢視mysql的安裝路徑,啟動、停止mysql服務

指令:find / -name mysql

顯示:

/var/lib/mysql

/var/lib/mysql/mysql

/etc/rc.d/init.d/mysql

/etc/logrotate.d/mysql

/usr/include/mysql

/usr/include/mysql/mysql

/usr/bin/mysql  

/usr/lib64/mysql

/usr/share/mysql

啟動服務:

指令:service mysql start 

關閉服務:

指令:service mysql stop

檢視服務:

指令:service mysql status

六丶修改mysql的安裝路徑

改變mysql資料庫的安裝目錄一般來說隻要修改資料的存放目錄即可,即修改目錄/var/lib/mysql/的存放路徑,現将mysql資料存放目錄修改為/usr/local/mysql

介紹下資料庫主要目錄

1、資料庫目錄

  /var/lib/mysql/

  2、配置檔案

  /usr/share/mysql(mysql.server指令及配置檔案)mysql5.5之後的預設安裝路徑,mysql5.5之前的是/usr/local/mysql

 3、相關指令

  /usr/bin(mysqladmin mysqldump等指令)

  4、啟動腳本

 /etc/rc.d/init.d/(啟動腳本檔案mysql的目錄)

檢視mysql

指令:cd /var/lib/mysql

顯示:

RPM_UPGRADE_HISTORY  

RPM_UPGRADE_MARKER-LAST 

你伺服器名(保密條例,求原諒~).err  

auto.cnf

ib_logfile0  

ib_logfile1  

ibdata1  

mysql  

mysql.sock  

performance_schema  

test

看舒服了嗎?那接下來

指令:cp -arp /var/lib/mysql  /usr/local

指令:service mysql stop

重頭戲來了:修改/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 = .....

#datadir =.....

# port = .....

# server_id = .....

#socket =.....

datadir =/usr/local/mysql

socket =/usr/local/mysql/mysql.sock

# 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

最後做一個軟連接配接:

指令:ln -s  /usr/local/mysql/mysql.sock  /var//lib/mysql/mysql.sock

七丶啟動服務,登入mysql(這裡有坑,同學們注意看哦)

MySQL的安裝日志資訊中,你會看到MySQL生成了root使用者的随機密碼(如下截圖所示),并将這個随機密碼放置在/root/.mysql_secret中。并且強制在第一次登陸時修改root使用者的密碼。Mysql 5.6及以後版本出處于安全考慮,root密碼不為空。是以在MySQL的安裝過程中,會生成一個随機密碼,該檔案為隐藏檔案。

指令:cd /root

指令:pwd

指令:ls -la

指令:cat .mysql_secret

你就可以看到了你的随機密碼。

BUT:老子沒有root權限,甚至沒有登入root使用者的密碼,那不是血崩?說什麼mysql免密登入,需要root 密碼啊,網上各種攻略一堆一堆的,沒用啊,咋辦呢?我有辦法,在剛才的/etc/my.cnf 裡做文章

解決方案:

指令:vi /etc/my.cnf

在[mysqld]的段中加上一句:skip-grant-tables 

例如: 

[mysqld] 

datadir=/var/lib/mysql 

socket=/var/lib/mysql/mysql.sock 

skip-name-resolve 

skip-grant-tables 

儲存并且退出vi。 

3.重新啟動mysqld 

指令:service mysql restart

Stopping MySQL: [ OK ] 

Starting MySQL: [ OK ] 

4.登入并修改MySQL的root密碼 

# /usr/bin/mysql 

Welcome to the MySQL monitor. Commands end with ; or \g. 

Your MySQL connection id is 3 to server version: 3.23.56 

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer. 

mysql> USE mysql ; 

Reading table information for completion of table and column names 

You can turn off this feature to get a quicker startup with -A 

Database changed 

mysql> UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’ ; 
(備注:mysql5.7以上修改密碼語句:         update              mysql.              user              set              authentication_string=              password              ('                new-password         ')               where              user              =              'root'              and              Host =               'localhost'              ;                )

Query OK, 0 rows affected (0.00 sec) 

Rows matched: 2 Changed: 0 Warnings: 0 

mysql> flush privileges ; 

Query OK, 0 rows affected (0.01 sec) 

mysql> quit 

Bye 

5.将MySQL的登入設定修改回來 

# vi /etc/my.cnf 

将剛才在[mysqld]的段中加上的skip-grant-tables删除 

儲存并且退出vi。 

6.重新啟動mysqld 

# /etc/init.d/mysqld restart 

Stopping MySQL: [ OK ] 

Starting MySQL: [ OK ]       

搞定了,不要膜拜哥,把膜拜藏在心裡就好!

這裡要注意了,可能有個坑:

利用安全模式成功登陸,然後修改密碼,等于給MySql設定了密碼。登陸進去後,想查詢所有存在的資料庫測試下。得到的結果确實:

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

非常詭異啊,明明用密碼登陸進去了,怎麼還提示需要密碼。 

參考官方的一個文檔,見http://dev.mysql.com/doc/refman/5.6/en/alter-user.html。如下操作後就ok了:

mysql> show databases; 

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

mysql> SET PASSWORD = PASSWORD('root'); 

Query OK, 0 rows affected (0.00 sec)

mysql> show databases; 

5 rows in set (0.00 sec)

最後一個坑:

資料庫安裝完,用navicat 登入遠端庫,提示

ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL ERROR 1062 (23000)

怎麼辦?

權限問題

解決方案:

use mysql

mysql> select host, user from user;

update user set host='%' where user='root';
           

報錯:ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' 不予理會

flush privileges;
           

重新遠端連接配接OK.沒坑了吧?