這裏咱們所說的安裝 mysql 其實是指安裝 mysql-server ,就是mysql的服務端,mysql 客戶端通常在 咱們的系統裏已經安裝好了,並不須要再次安裝了。不過咱們也能夠在安裝 mysql-server 時從新安裝 mysql 客戶端node
使用命令 yum list | grep mysql
查看 yum
上可用的 mysql 數據庫版本。mysql
yum list | grep mysql yum list mysql-server
使用 yum install mysql-server
安裝mysql服務端sql
➜ ~ yum install mysql-server ...此處省略一萬字 總下載量:12 M 肯定嗎?[y/N]:y 下載軟件包: (1/5): mysql-5.1.73-7.el6.i686.rpm | 904 kB 00:00 (2/5): mysql-libs-5.1.73-7.el6.i686.rpm | 1.2 MB 00:00 (3/5): mysql-server-5.1.73-7.el6.i686.rpm | 8.8 MB 00:00 (4/5): perl-DBD-MySQL-4.013-3.el6.i686.rpm | 134 kB 00:00 (5/5): perl-DBI-1.609-4.el6.i686.rpm | 705 kB 00:00 ------------------------------------------------------------------------- 總計 22 MB/s | 12 MB 00:00 運行 rpm_check_debug 執行事務測試 事務測試成功 執行事務 正在升級 : mysql-libs-5.1.73-7.el6.i686 1/6 正在安裝 : perl-DBI-1.609-4.el6.i686 2/6 正在安裝 : perl-DBD-MySQL-4.013-3.el6.i686 3/6 正在安裝 : mysql-5.1.73-7.el6.i686 4/6 正在安裝 : mysql-server-5.1.73-7.el6.i686 5/6 清理 : mysql-libs-5.1.73-5.el6_6.i686 6/6 Verifying : perl-DBD-MySQL-4.013-3.el6.i686 1/6 Verifying : mysql-server-5.1.73-7.el6.i686 2/6 Verifying : perl-DBI-1.609-4.el6.i686 3/6 Verifying : mysql-libs-5.1.73-7.el6.i686 4/6 Verifying : mysql-5.1.73-7.el6.i686 5/6 Verifying : mysql-libs-5.1.73-5.el6_6.i686 6/6 已安裝: mysql-server.i686 0:5.1.73-7.el6 做爲依賴被安裝: mysql.i686 0:5.1.73-7.el6 perl-DBD-MySQL.i686 0:4.013-3.el6 perl-DBI.i686 0:1.609-4.el6 做爲依賴被升級: mysql-libs.i686 0:5.1.73-7.el6 完畢!
到這裏就安裝完成了數據庫
當咱們第一次啓動 mysql 數據庫時,會進行一系列的初始化配置,輸出以下內容centos
➜ ~ service mysqld start Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h VM_176_3_centos password 'new-password' Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ] Starting mysqld: [ OK ]
這時, mysql 數據庫就啓動成功了,第二次啓動數據庫的時候 就不會再輸出這麼多內容了。服務器
重啓數據庫可使用如下命令:socket
➜ ~ service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
數據庫啓動成功後,設置 mysql 開機自啓動tcp
➜ ~ chkconfig mysqld on
能夠經過 chkconfig --list | grep mysql
命令查看 mysql 數據庫是不是開機自啓動ide
➜ ~ chkconfig --list | grep mysql mysqld 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
注意:chkconfig --list
能夠查看服務器中 全部服務的自啓動狀態測試
mysql數據庫安裝完之後只有一個root管理員帳號,可是此時的root帳號尚未爲其設置密碼,這時回頭看一下在第一次啓動mysql服務時,輸出的一大段信息中,咱們看到有這樣一行信息 :
/usr/bin/mysqladmin -u root password 'new-password' // 爲root帳號設置密碼
因此咱們能夠經過 該命令來給咱們的root帳號設置密碼(注意:這個root帳號是mysql的root帳號,非Linux的root帳號)
➜ ~ mysqladmin -u root password '123456' // 經過該命令給root帳號設置密碼爲 123456
而後就能夠經過 mysql -u root -p
命令來登陸 mysql 數據庫了
➜ ~ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> exit; Bye
使用rpm -qi mysql-server
命令查看安裝好的 mysql-server 版本信息
➜ ~ rpm -qi mysql-server Name : mysql-server Relocations: (not relocatable) Version : 5.1.73 Vendor: CentOS Release : 7.el6 Build Date: 2016年05月11日 星期三 14時05分53秒 Install Date: 2016年10月08日 星期六 14時49分09秒 Build Host: worker1.bsys.centos.org Group : Applications/Databases Source RPM: mysql-5.1.73-7.el6.src.rpm Size : 25688915 License: GPLv2 with exceptions Signature : RSA/SHA1, 2016年05月12日 星期四 18時46分25秒, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> URL : http://www.mysql.com Summary : The MySQL server and related files Description : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a client/server implementation consisting of a server daemon (mysqld) and many different client programs and libraries. This package contains the MySQL server and some accompanying files and directories.
咱們能夠經過以下命令來查看咱們的系統上是否 安裝了 mysql
➜ ~ rpm -qa | grep mysql mysql-5.1.73-7.el6.i686 mysql-libs-5.1.73-7.el6.i686 mysql-server-5.1.73-7.el6.i686
經過 rpm -e
命令 或者 rpm -e --nodeps
命令來卸載掉
➜ ~ rpm -qa | grep mysql mysql-5.1.73-7.el6.i686 mysql-libs-5.1.73-7.el6.i686 mysql-server-5.1.73-7.el6.i686 ➜ ~ rpm -e mysql error: Failed dependencies: mysql = 5.1.73-7.el6 is needed by (installed) mysql-server-5.1.73-7.el6.i686 ➜ ~ rpm -e --nodeps mysql ➜ ~ rpm -qa | grep mysql mysql-libs-5.1.73-7.el6.i686 mysql-server-5.1.73-7.el6.i686
如上面的所示,當執行 rpm -e mysql
時會由於依賴關係 而出現錯誤,因此可使用 rpm -e --nodeps mysql
來強制卸載,執行完以後 再查看安裝的 mysql
發現少了 mysql-5.1.73-7.el6.i686
。
若是想所有卸載,再次執行
➜ ~ rpm -e --nodeps mysql-libs ➜ ~ rpm -e --nodeps mysql-server
便可。
/etc/my.cnf
中咱們能夠看一下 內容:
➜ ~ cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
mysql 的數據庫文件存放路徑爲:/var/lib/mysql
➜ ~ ls -l /var/lib/mysql 總用量 20528 -rw-rw---- 1 mysql mysql 10485760 10月 8 15:11 ibdata1 -rw-rw---- 1 mysql mysql 5242880 10月 8 15:11 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 10月 8 14:50 ib_logfile1 drwx------ 2 mysql mysql 4096 10月 8 14:49 mysql srwxrwxrwx 1 mysql mysql 0 10月 8 15:11 mysql.sock drwx------ 2 mysql mysql 4096 10月 8 14:49 test
mysql數據庫的日誌輸出存放位置在 /var/log
這個目錄下
➜ ~ ls /var/log anaconda.ifcfg.log anaconda.yum.log dmesg messages spooler anaconda.log audit dmesg.old mysqld.log tallylog anaconda.program.log boot.log dracut.log ntpstats wtmp anaconda.storage.log btmp lastlog prelink yum.log anaconda.syslog cron maillog secure
其中mysqld.log 這個文件就是mysql數據庫產生的一些日誌信息,經過查看該日誌文件,咱們能夠從中得到不少信息
mysql 的服務端口爲 3306 ,咱們可使用 netstat -anp
命令來查看系統是否在監聽這個端口
➜ ~ netstat -anp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3513/mysqld
通過上面的設置後,還不能進行遠程訪問,鏈接的時候以下圖所示:
首先登陸 mysql
數據庫,而後使用命令grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
配置遠程訪問:
➜ ~ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> exit; Bye ➜ ~
再次測試,就OK了。
至此,全部的安裝配置都完成了,若有紕漏,請及時告知,謝謝~~