1. 程式人生 > >CentOS 7 系統安裝與遠端連線 MySQL 5.7

CentOS 7 系統安裝與遠端連線 MySQL 5.7

本文是滴滴雲開源框架教程系列文章的一篇。

CentOS 7 版本將MySQL資料庫軟體從預設的程式列表中移除,那麼CentOS 7版本如何安裝MySQL呢,本文將指導大傢俱體的方法。

滴滴雲DC2雲主機上的操作為例,首先切換賬戶許可權到root許可權,登入CentOS 7 系統的雲主機後輸入:

[[email protected] dc2-user]# sudo su

切換成root許可權,然後執行下面的安裝方法:

官網安裝mysql-server方法:

[[email protected] dc2-user]# yum install -y wget
[[email protected] dc2-user]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
[[email protected] dc2-user]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
[[email protected] dc2-user]# yum install mysql-community-server -y

啟動MySQL

[[email protected]
dc2-user]# systemctl start mysqld

驗證是否啟動

[[email protected] dc2-user]# systemctl status mysqld

MySQL安裝完成之後,在/var/log/mysqld.log檔案中給root生成了一個預設密碼。通過下面的方式找到root預設密碼,然後登入MySQL進行修改:

[[email protected] dc2-user]# grep 'temporary password' /var/log/mysqld.log
2018-05-23T09:24:03.800263Z 1 [Note] A temporary password is generated for 
[email protected]
: p?iT>b?iv7st

可以看到初始密碼為 p?iT>b?iv7st,登入資料庫:

[[email protected] dc2-user]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.22Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

重新設定root密碼

mysql> set password for 'root'@'localhost'=password('[email protected]');

為資料庫設定遠端連線

首先在防火牆規則中開啟3306埠

設定MySQL

mysql> grant all privileges on *.* to [email protected]'%'identified by '123456';

如果是非root使用者(mysql的root使用者)遠端登入,則要先建立使用者

mysql>create user 'username'@'%' identified by 'password';

然後再進行連線

[[email protected] dc2-user]#mysql -h <IP地址> -uusername -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.39 MySQL Community Server (GPL)
  
Copyright (c) 2000, 2017, 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>