日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Linux安装MySQL的两种方法

發布時間:2025/3/11 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux安装MySQL的两种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文鏈接:http://blog.csdn.net/superchanon/article/details/8546254

---------------------------------------------------------------------------------------


1.?????? 運行平臺:CentOS 6.3 x86_64,基本等同于RHEL 6.3

2.?????? 安裝方法:

安裝MySQL主要有兩種方法:一種是通過源碼自行編譯安裝,這種適合高級用戶定制MySQL的特性,這里不做說明;另一種是通過編譯過的二進制文件進行安裝。二進制文件安裝的方法又分為兩種:一種是不針對特定平臺的通用安裝方法,使用的二進制文件是后綴為.tar.gz的壓縮文件;第二種是使用RPM或其他包進行安裝,這種安裝進程會自動完成系統的相關配置,所以比較方便。

3.?????? 下載安裝包:

a.? 官方下載地址:

http://dev.mysql.com/downloads/mysql/#downloads

或鏡像文件下載:

http://dev.mysql.com/downloads/mirrors.html

2.? 下載文件(根據操作系統選擇相應的發布版本):

a.? 通用安裝方法

mysql-5.5.29-linux2.6-x86_64.tar.gz

b.?????? RPM安裝方法:

MySQL-server-5.5.29-2.el6.x86_64.rpm

MySQL-client-5.5.29-2.el6.x86_64.rpm

4.?????? 通用安裝步驟

a.?????? 檢查是否已安裝,grep的-i選項表示匹配時忽略大小寫

[root@localhost JavaEE]#rpm -qa|grep -i mysql

mysql-libs-5.1.61-4.el6.x86_64

*可見已經安裝了庫文件,應該先卸載,不然會出現覆蓋錯誤。注意卸:載時使用了--nodeps選項,忽略了依賴關系:

[root@localhost JavaEE]#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps

b.???? 添加mysql組和mysql用戶,用于設置mysql安裝目錄文件所有者和所屬組。

[root@localhost JavaEE]#groupadd mysql

[root@localhost JavaEE]#useradd -r -g mysql mysql

*useradd -r參數表示mysql用戶是系統用戶,不可用于登錄系統。

c.? 將二進制文件解壓到指定的安裝目錄,我們這里指定為/usr/local

[root@localhost ~]# cd/usr/local/

[root@localhost local]#tar zxvf /path/to/mysql-5.5.29-linux2.6-x86_64.tar.gz

*加壓后在/usr/local/生成了解壓后的文件夾mysql-5.5.29-linux2.6-x86_64,這名字太長,我們為它建立一個符號鏈接mysql,方便輸入。

[root@localhost local]#ln -s mysql-5.5.29-linux2.6-x86_64 mysql

d.???? /usr/local/mysql/下的目錄結構

Directory

Contents of Directory

bin

Client programs and the?mysqld?server

data

Log files, databases

docs

Manual in Info format

man

Unix manual pages

include

Include (header) files

lib

Libraries

scripts

mysql_install_db

share

Miscellaneous support files, including error messages, sample configuration files, SQL for database installation

sql-bench

Benchmarks

e.???? 進入mysql文件夾,也就是mysql所在的目錄,并更改所屬的組和用戶。

[root@localhost local]#cd mysql

[root@localhost mysql]#chown -R mysql .

[root@localhost mysql]#chgrp -R mysql .

f.?????? 執行mysql_install_db腳本,對mysql中的data目錄進行初始化并創建一些系統表格。注意mysql服務進程mysqld運行時會訪問data目錄,所以必須由啟動mysqld進程的用戶(就是我們之前設置的mysql用戶)執行這個腳本,或者用root執行,但是加上參數--user=mysql。

[root@localhost mysql]scripts/mysql_install_db --user=mysql

*如果mysql的安裝目錄(解壓目錄)不是/usr/local/mysql,那么還必須指定目錄參數,如

[root@localhost mysql]scripts/mysql_install_db --user=mysql \

???????? --basedir=/opt/mysql/mysql \

???????? --datadir=/opt/mysql/mysql/data

*將mysql/目錄下除了data/目錄的所有文件,改回root用戶所有,mysql用戶只需作為mysql/data/目錄下所有文件的所有者。

[root@localhost mysql]chown -R root .

[root@localhost mysql]chown -R mysql data

g.???? 復制配置文件

[root@localhost mysql] cp support-files/my-medium.cnf /etc/my.cnf

h.? 將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

i.? 重啟系統,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!

j.?????? 運行客戶端程序mysql,在mysql/bin目錄中,測試能否連接到mysqld。

[root@localhost mysql]#/usr/local/mysql/bin/mysql

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最后加入兩行命令:

MYSQL_HOME=/usr/local/mysql

export PATH=$PATH:$MYSQL_HOME/bin

這樣就可以在shell中直接輸入mysql命令來啟動客戶端程序了

[root@localhost mysql]#mysql

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>

?

第二種方法?

?RPM安裝步驟

a.?????? 檢查是否已安裝,grep的-i選項表示匹配時忽略大小寫

[root@localhost JavaEE]#rpm -qa|grep -i mysql

mysql-libs-5.1.61-4.el6.x86_64

可見已經安裝了庫文件,應該先卸載,不然會出現覆蓋錯誤。注意卸載時使用了--nodeps選項,忽略了依賴關系:

[root@localhost JavaEE]#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps

2.???? 安裝MySQL的服務器端軟件,注意切換到root用戶:

[root@localhost JavaEE]#rpm -ivh MySQL-server-5.5.29-2.el6.x86_64.rpm

安裝完成后,安裝進程會在Linux中添加一個mysql組,以及屬于mysql組的用戶mysql。可通過id命令查看:

[root@localhost JavaEE]#id mysql

uid=496(mysql)gid=493(mysql) groups=493(mysql)

MySQL服務器安裝之后雖然配置了相關文件,但并沒有自動啟動mysqld服務,需自行啟動:

[root@localhost JavaEE]#service mysql start

Starting MySQL.. SUCCESS!

可通過檢查端口是否開啟來查看MySQL是否正常啟動:

[root@localhost JavaEE]#netstat -anp|grep 3306

tcp??????? 0?????0 0.0.0.0:3306???????????????0.0.0.0:*?????????????????? LISTEN????? 34693/mysqld

c.? 安裝MySQL的客戶端軟件:

[root@localhost JavaEE]#rpm -ivh MySQL-client-5.5.29-2.el6.x86_64.rpm

如果安裝成功應該可以運行mysql命令,注意必須是mysqld服務以及開啟:

[root@localhost JavaEE]#mysql

Welcome to the MySQLmonitor.? Commands end with ; or \g.

Your MySQL connection idis 1

Server version: 5.5.29MySQL Community Server (GPL)

Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademarkof Oracle Corporation and/or its affiliates. Other names may be trademarks oftheir respective owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql>

d.? RPM安裝方式文件分布

Directory

Contents of Directory

/usr/bin

Client programs and scripts

/usr/sbin

The?mysqld?server

/var/lib/mysql

Log files, databases

/usr/share/info

Manual in Info format

/usr/share/man

Unix manual pages

/usr/include/mysql

Include (header) files

/usr/lib/mysql

Libraries

/usr/share/mysql

Miscellaneous support files, including error messages, character set files, sample configuration files, SQL for database installation

/usr/share/sql-bench

Benchmarks

?

?

---------2016-08-26--松門一枝花補充--------

但我安裝時報錯:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

-----安裝好serve的時候,有下面的提示:-----

適合5.7之前的版本。之后的請移步此處

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.


You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.


Also, the account for the anonymous user has been removed.


In addition, you can run:


? /usr/bin/mysql_secure_installation


which will also give you the option of removing the test database.
This is strongly recommended for production servers.


See the manual for more instructions.


Please report any problems at http://bugs.mysql.com/


The latest information about MySQL is available on the web at


? http://www.mysql.com


Support MySQL by buying support/licenses at http://shop.mysql.com


New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings


進入:mysql_secret?

適合5.7之前的版本。之后的請移步此處

[root@hello110 ~]# cat .mysql_secret?
# The random password set for the root user at Sat Aug 27 01:33:41 2016 (local time): RsD3_afVuO4zOSxi


用里面的密碼登錄:


[root@hello110 hello]# mysql -uroot -pRsD3_afVuO4zOSxi ? ??

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. ?Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.32


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


mysql> set password = password('123456');
Query OK, 0 rows affected (0.16 sec)


修改密碼后,就可以使用了。


mysql>?



總結

以上是生活随笔為你收集整理的Linux安装MySQL的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。