Centos7安裝mysql

CentOS7的yum源中默認好像是沒有mysql的。爲了解決這個問題,咱們要先下載mysql的repo源。mysql

1. 下載mysql的repo源

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2. 安裝mysql-community-release-el7-5.noarch.rpm包

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

    安裝這個包後,會得到兩個mysql的yum repo源:sql

/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo

3. 安裝mysql

$ sudo yum install mysql-server

    根據步驟安裝就能夠了,不過安裝完成後,沒有密碼,須要重置密碼。數據庫

4. 重置密碼

    重置密碼前,首先要登陸vim

$ 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的擁有者改成當前用戶:centos

$ sudo chown -R root:root /var/lib/mysql

    而後,重啓服務:服務器

$ service mysqld restart

    接下來登陸重置密碼:socket

$ mysql -u root
mysql > use mysql;
mysql > update user set password=password('admins') where user='root';
mysql > use mysql;
mysql > grant all on root.* to 'root'@'%' identified by 'admins';
mysql > flush privileges;
mysql > exit;
$ mysql -u root -p
Enter password: 
mysql > update user set password=password("zsjAdmin") where user="root";
$ systemctl restart mysql.service

注:執行上面的命令計劃就安裝完成了,遠程能夠進行訪問。tcp

建立表時報錯1044說名你權限不足ide

grant all privileges on *.* to root@localhost identified by 'mysql'

5. 開放3306端口

    $ sudo vim /etc/sysconfig/iptablescentos7

    添加如下內容:

    -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 root.* to 'root'@'localhost'

    identified by 'admins';

    #設置openscanner用戶與密碼,並從任何機器均可以訪問mysql

    mysql > grant all on root.* to 'root'@'%' identified by 'admins';

    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客戶端的。

grant all privileges on *.* to root@'%' identified by "admins";

相關文章
相關標籤/搜索