Linux下安裝mysql

1、首先關閉linux的防火牆,執行命令

chkconfig iptables off

2、從mysql官網上下載自己適合的mysql版本https://dev.mysql.com/downloads/mysql/5.6.html#downloads,進入mysql官網,依次點擊

 

 

 

 

 

3、下載後的mysql文件

mysql-5.6.40-linux-glibc2.12-i686.tar.gz

 將下載好的mysql壓縮文件放置在linux的/usr/local文件夾下,解壓該壓縮文件

 tar -zxvf mysql-5.6.40-linux-glibc2.12-i686.tar.gz

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

mv mysql-5.6.40-linux-glibc2.12-i686 mysql

4、創建mysql用戶組及用戶

groupadd mysql
useradd -r -g mysql mysql

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

cp support-files/my-medium.cnf /etc/my.cnf
或:
cp support-files/my-default.cnf /etc/my.cnf
是否覆蓋?按y 回車

6、編輯/etc/my.cnf文件;

vi /etc/my.cnf

在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 = /usr/local/mysql/data/log

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
socket = /tmp/mysql.sock
pid_file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp
log_err = /usr/local/mysql/data/log/error.log
#lower_case_table_names 大小寫敏感設置的屬性 
#lower_case_table_names=1:表名存儲在磁盤是小寫的,但是比較的時候是不區分大小寫
#lower_case_table_names=0:表名存儲爲給定的大小和比較是區分大小寫的
#lower_case_table_names=2:表名存儲爲給定的大小寫但是比較的時候是小寫的
lower_case_table_names = 1
character-set-server = utf8
skip-name-resolve
#bind_address = 127.0.0.1

# 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

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

chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data (別忘記設置訪問權限)

 

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

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

 

9、啓動mysql,進入/usr/local/mysql/bin目錄,執行下面命令

./mysqld_safe --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root &
注意,如果光標停留在屏幕上,表示啓動成功,需要我們先關閉shell終端,再開啓一個新的shell終端,不要執行退出操作。如果出現 mysql ended這樣的語句,表示Mysql沒有正常啓動,你可以到log中查找問題. 

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

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /usr/local/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 /usr/local/mysql/

重啓linux

reboot

查看mysql狀態

service mysqld status

如果mysql未啓動則執行命令:service mysql start

執行命令:ps -ef|grep mysql 看到mysql服務是否啓動成功

11、添加遠程訪問權限

  (1)、添加mysql命令

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

  (2)、更改訪問權限

登錄mysql,執行下面命令
mysql -uroot -p 
密碼爲空直接回車,運行以下兩條命令(使用root/123456從任何主機連接到mysql服務器)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' with grant option;
Flush privileges;

退出mysql

exit

重啓linux,就完成了

reboot

注:本機訪問mysql,root賬戶默認是沒有密碼的,端口號默認3306,如果需要修改root賬戶密碼,在/usr/local/mysql/bin目錄下,執行下面命令

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

安裝完後的使用

安裝完以後,大家可以輸入mysql -uroot -p,然後enter password來進入mysql。

進去後可以使用mysql的各種語法,首先可以查看database:show databases。

 

Linux下mysql的卸載:

1、查找以前是否裝有mysql

命令:rpm -qa|grep -i mysql

可以看到mysql的兩個包:

mysql-4.1.12-3.RHEL4.1

mysqlclient10-3.23.58-4.RHEL4.1

2、刪除mysql

刪除命令:rpm -e --nodeps 包名

( rpm -ev mysql-4.1.12-3.RHEL4.1 )

3、刪除老版本mysql的開發頭文件和庫

檢查各個mysql文件夾是否刪除乾淨

find / -name mysql

結果如下:

/var/lib/mysql

/usr/local/mysql

/usr/lib/mysql

/usr/include/mysql

命令:

rm -fr /usr/lib/mysql

rm -fr /usr/include/mysql

注意:卸載後/var/lib/mysql中的數據及/etc/my.cnf不會刪除,如果確定沒用後就手工刪除

rm -f /etc/my.cnf

rm -fr /var/lib/mysql

 4.刪除mysql用戶及用戶組

 userdel mysql

groupdel mysql

 

Linux下創建和刪除軟鏈接:

1.先建立一個軟連接

 1 [[email protected] test]# ls -il
 2 總計  0
 3 1491138 -rw-r–r– 1 root root 48 07-14 14:17 file1
 4 1491139 -rw-r–r– 2  root root 0 07-14 14:17 file2
 5 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2hand
 6 #建立file1和file1soft軟連接
 7 [[email protected] test]# ln -s file1  file1soft
 8 [[email protected] test]# ls -il
 9 總計 0
10 1491138 -rw-r–r– 1 root  root 48 07-14 14:17 file1
11 1491140 lrwxrwxrwx 1 root root 5 07-14 14:24  file1soft -> file1
12 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2
13 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand

其中,ln -s file1 filesoft 中的file1就是源文件,file1soft就是目標鏈接文件名,其作用是當進入filesoft目錄,實際上是鏈接進入了file1目錄

2.刪除上面建立的軟連接

 1 [[email protected] test]# ls -il
 2 總計  0
 3 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1
 4 1491140 lrwxrwxrwx 1  root root 5 07-14 14:24 file1soft -> file1
 5 1491139 -rw-r–r– 2 root root 0  07-14 14:17 file2
 6 1491139 -rw-r–r– 2 root root 0 07-14 14:17  file2hand
 7 #刪除軟連接
 8 [[email protected] test]# rm -rf file1soft
 9 [[email protected] test]#  ls -il
10 總計 0
11 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1
12 1491139  -rw-r–r– 2 root root 0 07-14 14:17 file2
13 1491139 -rw-r–r– 2 root root 0 07-14  14:17 file2hand

 

啓動mysql時顯示:/tmp/mysql.sock 不存在的解決方法

1 [root@localhost mysql]# bin/mysqladmin -u root password root
2 bin/mysqladmin: connect to server at 'localhost' failed
3 error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
4 Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
5 [root@localhost mysql]# bin/mysql -u root -p
6 Enter password:
7 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
8 分析:是/tmp/mysql.sock 不存在

由於搜索的mysql.sock路徑是在/tmp下,而mysql安裝的mysql.sock在/var/lib/mysql下,所以選擇建立符號(軟)連接:

1 # ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
2 # bin/mysql -u root
3 Welcome to the MySQL monitor. Commands end with ; or g.
4 Your MySQL connection id is 1
5 Server version: 5.0.45 MySQL Community Server (GPL)
6 Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
7 mysql>

安裝mysql報錯:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper

解決方法 :安裝autoconf庫

命令:yum-y install autoconf   //此包安裝時會安裝Data:Dumper模塊

 

啓動mysql報錯:Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/cnsz22VLK15556.pid).

1、可能是/usr/local/mysql/data/mysql.pid文件沒有寫的權限
解決方法 :給予權限,執行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data”  然後重新啓動mysqld!

2、可能進程裏已經存在mysql進程
解決方法:用命令“ps -ef|grep mysqld”查看是否有mysqld進程,如果有使用“kill -9  進程號”殺死,然後重新啓動mysqld!

3、可能是第二次在機器上安裝mysql,有殘餘數據影響了服務的啓動。
解決方法:去mysql的數據目錄/data看看,如果存在mysql-bin.index,就趕快把它刪除掉吧,它就是罪魁禍首了。

4、mysql在啓動時沒有指定配置文件時會使用/etc/my.cnf配置文件,請打開這個文件查看在[mysqld]節下有沒有指定數據目錄(datadir)。
解決方法:請在[mysqld]下設置這一行:datadir = /usr/local/mysql/data

5、skip-federated字段問題
解決方法:檢查一下/etc/my.cnf文件中有沒有沒被註釋掉的skip-federated字段,如果有就立即註釋掉吧。

6、錯誤日誌目錄不存在。(本人是這個方法解決的)
解決方法:使用“chown” “chmod”命令賦予mysql所有者及權限

7、selinux惹的禍,如果是centos系統,默認會開啓selinux
解決方法:關閉它,打開/etc/selinux/config,把SELINUX=enforcing改爲SELINUX=disabled後存盤退出重啓機器試試。

轉自:https://www.cnblogs.com/fnlingnzb-learner/p/5830622.html

希望此文能夠幫助到您!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章