在CentOS中默認安裝有MariaDB,這個是MySQL的分支,但為了需要,還是要在系統中安裝MySQL,而且安裝完成之后可以直接覆蓋掉MariaDB。
1 下載並安裝MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下載了安裝用的Yum Repository,大概25KB的樣子,然后就可以直接yum安裝了。
yum -y install mysql57-community-release-el7-10.noarch.rpm
之后就開始安裝MySQL服務器。
yum -y install mysql-community-server
這步可能會花些時間,安裝完成后就會覆蓋掉之前的mariadb。

至此MySQL就安裝完成了,然后是對MySQL的一些設置。
2 MySQL數據庫設置
首先啟動MySQL
[root@localhost ~]# systemctl start mysqld.service
查看MySQL運行狀態,運行狀態如圖:
systemctl status mysqld.service

此時MySQL已經開始正常運行,不過要想進入MySQL還得先找出此時root用戶的密碼,通過如下命令可以在日志文件中找出密碼:
grep "password" /var/log/mysqld.log

如下命令進入數據庫:
mysql -u root -p
輸入初始密碼,此時不能做任何事情,因為MySQL默認必須修改密碼之后才能操作數據庫:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
這里有個問題,新密碼設置的時候如果設置的過於簡單會報錯:

原因是因為MySQL有密碼設置的規范,具體是與validate_password_policy的值有關:

MySQL完整的初始密碼規則可以通過如下命令查看:
SHOW VARIABLES LIKE 'validate_password%';

密碼的長度是由validate_password_length決定的,而validate_password_length的計算公式是:
validate_password_length = validate_password_number_count +validate_password_special_char_count + (2* validate_password_mixed_case_count)
我的是已經修改過的,初始情況下第一個的值是ON,validate_password_length是8。可以通過如下命令修改:
mysql> set global validate_password_policy=0; mysql> set global validate_password_length=1;
設置之后就是我上面查出來的那幾個值了,此時密碼就可以設置的很簡單,例如1234之類的。到此數據庫的密碼設置就完成了。
但此時還有一個問題,就是因為安裝了Yum Repository,以后每次yum操作都會自動更新,需要把這個卸載掉:
[root@localhost ~]#yum -y remove mysql57-community-release-el7-10.noarch
此時才算真的完成了。
mysql允許外部鏈接
mysql -u root -p mysql>use mysql; mysql>select 'host' from user where user='root'; mysql>update user set host = '%' where user ='root'; mysql>flush privileges; mysql>select 'host' from user where user='root';
第一句是以權限用戶root登錄
第二句:選擇mysql庫
第三句:查看mysql庫中的user表的host值(即可進行連接訪問的主機/IP名稱)
第四句:修改host值(以通配符%的內容增加主機/IP地址),當然也可以直接增加IP地址
第五句:刷新MySQL的系統權限相關表
第六句:再重新查看user表時,有修改。。
重起mysql服務即可完成。
打開mysql配置文件vi /etc/mysql/mysql.conf.d/mysqld.cnf
將bind-address = 127.0.0.1注銷
bind-address = 0.0.0.0 # 表示允許任何主機登陸MySQL
port=3306 # 表示MySQL運行端口為3306
2、支持root用戶允許遠程連接mysql數據庫
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; flush privileges; /etc/init.d/mysql start
5.6 5.7 密碼及權限設置
mysql5.6 grant all privileges on *.* to root@'%' identified by "mysql_pwd" with grant option; grant all privileges on *.* to root@'localhost' identified by "mysql_pwd" with grant option; grant all privileges on *.* to root@'127.0.0.1' identified by "mysql_pwd" with grant option; drop database if exists test; use mysql; delete from user where not (user='root'); delete from db where user=''; UPDATE user SET password=PASSWORD('mysql_pwd') WHERE user='root' AND host='127.0.0.1' OR host='%' OR host='localhost'; delete from user where password=''; flush privileges; select user,password,host from mysql.user; exit; mysql5.7: grant all privileges on *.* to root@'%' identified by "mysql_pwd" with grant option; grant all privileges on *.* to root@'localhost' identified by "mysql_pwd" with grant option; grant all privileges on *.* to root@'127.0.0.1' identified by "mysql_pwd" with grant option; drop database if exists test; use mysql; delete from user where not (user='root'); delete from db where user=''; delete from user where authentication_string=''; update mysql.user set authentication_string=password('mysql_pwd') where user='root' AND host='127.0.0.1' OR host='%' OR host='localhost'; flush privileges; select user,authentication_string,host from mysql.user; exit;
作者:小梨的十三
鏈接:https://www.jianshu.com/p/ad76ac7db32b
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。