CentOS 7安裝Mysql

CentOS 7安裝MySql

1. 查看mysql源安裝包

在MySQL官網中下載YUM源rpm安裝包:[http://dev.mysql.com/downloads/repo/yum/]html

2. 下載mysql源安裝包

shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

3. 安裝mysql源

shell> yum localinstall mysql57-community-release-el7-9.noarch.rpm

4. 檢查mysql源是否安裝成功

shell> yum repolist enabled | grep "mysql.*-community.*"
[root@localhost etc]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64        MySQL Connectors Community           30
mysql-tools-community/x86_64             MySQL Tools Community                40
mysql57-community/x86_64                 MySQL 5.7 Community Server          164
[root@localhost etc]#

5. 看到上所示表示安裝成功。

6. 能夠修改源,改變默認安裝的mysql版本。

shell> vim /etc/yum.repos.d/mysql-community.repo

7. 好比要安裝5.6版本,將5.7源的enabled=1改爲enabled=0。而後再將5.6源的enabled=0改爲enabled=1便可。enabled=1=安裝 enabled=0不安裝

8. 安裝MySql

shell> yum install mysql-community-server

9. 啓動MySql

shell> systemctl start mysqldmysql

10. 查看MySQL的啓動狀態

shell> systemctl status mysqld

如下內容表示成功sql

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor 
preset: disabled)
   Active: active (running) since 日 2017-01-01 20:49:56 CST; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 4866 ExecStart=/usr/sbin/mysqld --daemonize 
  --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited,       
  status=0/SUCCESS)
  Process: 4784 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, 
  status=0/SUCCESS)
 Main PID: 4869 (mysqld)
   Memory: 314.6M
   CGroup: /system.slice/mysqld.service
           └─4869 /usr/sbin/mysqld --daemonize 
           --pid-file=/var/run/mysqld/mysqld.pid
1月 01 20:49:40 localhost.localdomain systemd[1]: Starting MySQL Server...
1月 01 20:49:56 localhost.localdomain systemd[1]: Started MySQL Server.

11. 開機啓動

shell> systemctl enable mysqld
 shell> systemctl daemon-reload

12. 修改root本地登陸密碼

mysql安裝完成以後,在/var/log/mysqld.log文件中給root生成了一個默認密碼。經過下面的方式找到root默認密碼,而後
登陸mysql進行修改:

shell> grep 'temporary password' /var/log/mysqld.logshell

2017-01-01T12:49:48.897043Z 1 [Note] A temporary password is generated for
root@localhost: eCMf)m6Z&?b>
eCMf)m6Z&?b> 爲MySql ROOT用戶密碼
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
 
或者

mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
注意:mysql5.7默認安裝了密碼安全檢查插件(validate_password),默認密碼檢查策
略要求密碼必須包含:大小寫字母、數字和特殊符號,而且長度不能少於8位。不然會提
示ERROR 1819 (HY000): Your password does not satisfy the current policy 
requirements錯誤,

13. 經過msyql環境變量能夠查看密碼策略的相關信息:

mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name                         | Value  |
+---------------------------------------+--------+
| default_password_lifetime             | 0      |
| disconnect_on_expired_password        | ON     |
| log_builtin_as_identified_by_password | OFF    |
| mysql_native_password_proxy_users     | OFF    |
| old_passwords                         | 0      |
| report_password                       |        |
| sha256_password_proxy_users           | OFF    |
| validate_password_check_user_name     | OFF    |
| validate_password_dictionary_file     |        |
| validate_password_length              | 8      |
| validate_password_mixed_case_count    | 1      |
| validate_password_number_count        | 1      |
| validate_password_policy              | MEDIUM |
| validate_password_special_char_count  | 1      |
+---------------------------------------+--------+
14 rows in set (0.01 sec)

validate_password_policy:密碼策略,默認爲MEDIUM策略
validate_password_dictionary_file:密碼策略文件,策略爲STRONG才須要
validate_password_length:密碼最少長度
validate_password_mixed_case_count:大小寫字符長度,至少1個
validate_password_number_count :數字至少1個
validate_password_special_char_count:特殊字符至少1個
上述參數是默認策略MEDIUM的密碼檢查規則。

共有如下幾種密碼策略:
策略	        檢查規則
0 or LOW 	Length
1 or MEDIUM 	Length; numeric, lowercase/uppercase, and special characters
2 or STRONG 	Length; numeric, lowercase/uppercase, and special characters; 
dictionary file

MySQL官網密碼策略詳細說明:http://dev.mysql.com/doc/refman/5.7/en/validate-pa
ssword-options-variables.html#sysvar_validate_password_policy

14. 修改密碼策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密碼策略

# 選擇0(LOW),1(MEDIUM),2(STRONG)其中一種,選擇2須要提供密碼字典文件
validate_password_policy=0

若是不須要密碼策略,添加my.cnf文件中添加以下配置禁用便可:

validate_password = off

從新啓動mysql服務使配置生效:
shell>systemctl restart mysqld

15. 添加遠程登陸用戶

默認只容許root賬戶在本地登陸,若是要在其它機器上鍊接mysql,必須修改root容許遠
程鏈接,或者添加一個容許遠程鏈接的賬戶,爲了安全起見,我添加一個新的賬戶:

mysql> grant all privileges on . to 'root'@'%' identified by 'MyNewPass4!' with grant option; 或者 mysql>GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;數據庫

16. 配置默認編碼爲utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,以下所示:
vi /etc/my.cnf
添加如下代碼
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

mysql> show variables like '%character%';vim

從新啓動mysql服務,查看數據庫默認編碼以下所示:
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
相關文章
相關標籤/搜索