CentOS7的yum源中默認好像是沒有mysql的。爲了解決這個問題,咱們要先下載mysql的repo源。mysql
1. 下載mysql的repo源sql
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2. 安裝mysql-community-release-el7-5.noarch.rpm包shell
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
安裝這個包後,會得到兩個mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。數據庫
3. 安裝mysqlvim
$ sudo yum install mysql-server
根據步驟安裝就能夠了,不過安裝完成後,沒有密碼,須要重置密碼。centos
4. 重置密碼服務器
重置密碼前,首先要登陸socket
$ mysql -u root
登陸時有可能報這樣的錯:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2),緣由是/var/lib/mysql的訪問權限問題。下面的命令把/var/lib/mysql的擁有者改成當前用戶:tcp
$ sudo chown -R openscanner:openscanner /var/lib/mysql
而後,重啓服務:ide
$ service mysqld restart
接下來登陸重置密碼:
$ mysql -u root
mysql > use mysql; mysql > update user set password=password('123456') where user='root'; mysql > exit;
5. 開放3306端口
$ sudo vim /etc/sysconfig/iptables
添加如下內容:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
保存後重啓防火牆:
$ sudo service iptables restart
6. 建立普通用戶並受權
示例(使用root用戶登陸,並假定已經建立了openscannerstore數據庫):
mysql > use mysql;
#建立openscanner用戶與密碼並設置爲從安裝mysql服務的機器本地訪問 mysql > grant all on openscannerstore.* to 'openscanner'@'localhost' identified by 'scanner888';
#設置openscanner用戶與密碼,並從任何機器均可以訪問mysql mysql > grant all on openscannerstore.* to 'openscanner'@'%' identified by 'scanner888';
mysql > flush privileges; #刷新纔會生效
如今就能夠從客戶機鏈接mysql服務器了,若是鏈接報這樣的錯:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.x.xxx' (113)。由於咱們是centos7,請先確認防火牆是否開啓來,centos7默認是firewall,咱們能夠把它中止並禁止使用,而後啓動咱們熟悉的iptables,這樣就行了!
注:mysql客戶機是須要安裝mysql客戶端的。