環境:centos6.8 32位 本教程安裝MySQL是通過編譯過的二進制文件進行安裝。是不針對特定平台的通用安裝方法,使用的二進制文件是后綴為.tar.gz的壓縮文件 1、下載 http://dev.mysql.com/downloads/mysql/
下載到Windows 中,通過wincap軟件傳入linxu系統上,
2、解壓縮到/usr/local/下面,mysql的主目錄命名為mysql
如果是.tar.gz文件直接解壓 我下載的是.tar 文件 執行
[root@localhost local]# tar -xvf mysql-5.7.16-linux-glibc2.5-i686.tar
[root@localhost local]# tar xvf mysql-5.7.16-linux-glibc2.5-i686.tar.gz
[root@localhost local]# tar xvf mysql-5.7.16-linux-glibc2.5-i686.tar.gz
解壓后的文件夾重命名
[root@localhost local]# mv mysql-5.7.16-linux-glibc2.5-i686 /mysql
3、在mysql下面創建data數據庫文件目錄
[root@localhost local]# mkdir mysql/data
4、創建mysql的用戶組和用戶,並對mysql目錄設置用戶組和用戶
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd mysql -g mysql
[root@localhost local]# cd mysql
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .
5、初始化mysql並啟動mysql服務
[root@localhost mysql]# cd /usr/local/mysql/bin
[root@localhost bin]# yum install libaio
已加載插件:fastestmirror, refresh-packagekit, security
設置安裝進程
Loading mirror speeds from cached hostfile
* base: mirrors.opencas.cn
* extras: mirrors.btte.net
* updates: mirrors.btte.net
包 libaio-0.3.107-10.el6.i686 已安裝並且是最新版本
無須任何處理
[root@localhost bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2016-08-11 12:00:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-08-11 12:00:31 [WARNING] The bootstrap log isn't empty:
2016-08-11 12:00:31 [WARNING] 2016-01-09T04:00:29.262989Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-08-11T04:00:29.264643Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-08-11T04:00:29.264653Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
[root@localhost bin]# cd /usr/local/mysql/support-files
[root@localhost support-files]# ./mysql.server start
Starting MySQL.... [確定]
以上操作流程如下圖
6、登錄mysql,此版本最新版不許空密碼登錄,實際上有個初始化密碼保存在/root/.mysql_secret這個文件里面,用這個密碼第一次登錄后,再修改密碼。因此先cat查看下初始化密碼(隨機的,每次安裝看到的密碼都不一樣):
[root@localhost support-files]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2016-10-30 21:17:12 q<.yhitwgg!V [root@localhost support-files]# cd /usr/local/mysql/bin [root@localhost bin]# mysql -uroot -p
此處特別說明 如果出現-bash: mysql: command not found 錯誤 出下這種錯誤一般是由於安裝了一鍵安裝腳本后沒有對命令做鏈接,可以用下面的命令找到mysql命令的絕對路徑:
假設查找到的路徑為/usr/local/mysql/bin/mysql,可以使用下面的命令設置一個鏈接:
然后就可以使用mysql命令了。 |
利用初始化密碼q<.yhitwgg!V登錄mysql 操作如下圖:
顯示登錄成功,可以執行mysql命令操作了!
未配置的情況下每次登錄需要進入bin目錄下操作:
7、 復制配置文件
[root@localhost mysql] cp support-files/my-medium.cnf /etc/my.cnf
此處特別說明 如果出現 下面這種錯誤 找一下my-default.cnf 文件的位置 [root@localhost ~]# find / -name my-default.cnf 比如在(/usr/local/mysql/support-files/my-default.cnf)位置 作如下操作: [root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf |
8. 將mysqld服務加入開機自啟動項。
*首先需要將scripts/mysql.server服務腳本復制到/etc/init.d/,並重命名為mysqld。
[root@localhostmysql] cp support-files/mysql.server /etc/init.d/mysqld
*通過chkconfig命令將mysqld服務加入到自啟動服務項中。
[root@localhost mysql]#chkconfig --add mysqld
*注意服務名稱mysqld就是我們將mysql.server復制到/etc/init.d/時重命名的名稱。
*查看是否添加成功
[root@localhost mysql]#chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
9. 重啟系統,mysqld就會自動啟動了。
*檢查是否啟動
[root@localhost mysql]#netstat -anp|grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2365/mysqld
unix 2 [ ACC ] STREAM LISTENING 14396 2365/mysqld /tmp/mysql.sock
*如果不想重新啟動,那可以直接手動啟動。
[root@localhost mysql]#service mysqld start
Starting MySQL.. SUCCESS!
10 運行客戶端程序mysql,在mysql/bin目錄中,測試能否連接到mysqld。
[root@localhost mysql]#/usr/local/mysql/bin/mysql -uroot -p
password: Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 2
Server version:5.5.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.
Oracle is a registeredtrademark of Oracle Corporation and/or its affiliates. Other names may betrademarks of their respective owners.
Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.
mysql> quit
Bye
*此時會出現mysql>命令提示符,可以輸入sql語句,輸入quit或exit退出。為了避免每次都輸入mysql的全路徑/usr/local/mysql/bin/mysql,可將其加入環境變量中,在/etc/profile最后加入兩行命令:
- 方法一:
- 在/etc/profile文件中最后一行添加變量export PATH=$PATH:/usr/local/mysql/bin 【對所有用戶生效(永久的)】
- 用VI在文件/etc/profile文件中增加變量,該變量將會對Linux下所有用戶有效,並且是“永久的”。
- 要讓剛才的修改馬上生效,需要執行以下代碼
- # source /etc/profile
- 方法二:
- 在用戶目錄下的.bash_profile文件中增加變量【對單一用戶生效(永久的)】
- 用VI在用戶目錄下的.bash_profile文件中增加變量,改變量僅會對當前用戶有效,並且是“永久的”。
- 要讓剛才的修改馬上生效,需要在用戶目錄下執行以下代碼
- # source .bash_profile
- 方法三:
- 直接運行export命令定義變量【只對當前shell(BASH)有效(臨時的)】
- 在shell的命令行下直接使用[export變量名=變量值]定義變量,該變量只在當前的shell(BASH)或其子shell(BASH)下是有效的,shell關閉了,變量也就失效了,再打開新shell時就沒有這個變量,需要使用的話還需要重新定義。
- 例如:export PATH=$PATH:/usr/local/mysql/bin
這樣就可以在shell中直接輸入mysql命令來啟動客戶端程序了
[root@localhost mysql]#mysql -uroot -p
password: Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 3
Server version:5.5.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.
Oracle is a registeredtrademark of Oracle Corporation and/or its
affiliates. Other namesmay be trademarks of their respective
owners.
Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.
mysql>
11、改mysql的root密碼,新密碼在此為'123456'
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
12、設定遠程登錄mysql。在Linux下為了安全,默認是不允許mysql本機以外的機器訪問mysql數據庫服務,因此需要重新授權root。方便遠程訪問。
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> select Host,User from user;
+-----------+-----------+
| Host | User |
+-----------+-----------+
| % | root |
| localhost | mysql.sys |
| localhost | root |
+-----------+-----------+
3 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' identified by '000000';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
授權語句最后的‘000000’是mysql數據庫root用戶的新密碼。
13、非必要的步驟,如果遠程連不上,估計是防火牆的問題,關閉試試:
[root@localhost mysql]# service iptables stop
setenforce 0iptables:將鏈設置為政策 ACCEPT:filter [確定]
iptables:清除防火牆規則: [確定]
iptables:正在卸載模塊: [確定]
[root@localhost mysql]# setenforce 0
setenforce: SELinux is disabl