云服務(wù)器:阿里云CentOS7.3 64
MySQL的安裝會因?yàn)榘姹镜牟煌霈F(xiàn)不同的安裝和設(shè)置方式,以下以MySQL5.7版本做演示,應(yīng)該算是比較簡單的安裝方式了,按照步驟來不會出現(xiàn)各種坑。
1、下載安裝mysql
[root@sihan ~]# wget http://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
[root@sihan ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
[root@sihan ~]# yum -y install mysql-community-server
2、安裝成功后可查看mysql版本看是否安裝成功:
[root@sihan ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 86
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 79
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 327
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
enabled為當(dāng)前啟動的,如果想啟動其他禁止的,可使用一下命令
[root@sihan ~]# yum-config-manager --disable mysql57-community
[root@sihan ~]# yum-config-manager --enable mysql80-community
3、嘗試啟動mysql
[root@sihan ~]# systemctl start mysqld
[root@sihan ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2019-01-31 20:35:49 CST; 15s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 19663 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 19590 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 19668 (mysqld)
CGroup: /system.slice/mysqld.service
└─19668 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jan 31 20:35:43 sihan systemd[1]: Starting MySQL Server...
Jan 31 20:35:49 sihan systemd[1]: Started MySQL Server.
停止 MySQL
[root@sihan ~]# systemctl stop mysqld
4、修改默認(rèn)密碼
mysql安裝完成之后,在/var/log/mysqld.log文件中給root生成了一個(gè)默認(rèn)密碼。(mysql5.6密碼為空直接可以進(jìn)入,mysql5.7需要初始密碼)
[root@sihan ~]# less /var/log/mysqld.log
2019-01-31T12:35:44.031805Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-01-31T12:35:45.029115Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-01-31T12:35:45.151260Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-01-31T12:35:45.215310Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: bac0408d-2554-11e9-8094-00163e0ee124.
2019-01-31T12:35:45.219856Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-01-31T12:35:45.220371Z 1 [Note] A temporary password is generated for root@localhost: 3fig6nNyqA(k
使用默認(rèn)密碼登錄進(jìn)去并修改mysql密碼,并刷新權(quán)限(mysql5.7對密碼復(fù)雜度會進(jìn)行校驗(yàn)。必須含有數(shù)字,小寫或大寫字母,特殊字符;長度要不小于8位。)
[root@sihan ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by 'Password123@';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
如果需要設(shè)置諸如123456之類的簡單密碼,也是有辦法可行的。
首先,修改validate_password_policy參數(shù)的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
validate_password_length(密碼長度)參數(shù)默認(rèn)為8,我們修改為6
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
這樣便可以修改簡單密碼啦。
5、遠(yuǎn)程連接mysql
1、mysql默認(rèn)的端口是3306,一般新服務(wù)器是沒有對此端口開放的,所以需要自行上云提供商后臺配置安全組規(guī)則,開放3306端口。
開放后會發(fā)現(xiàn)連接還是不行,報(bào)錯(cuò)如下圖所示:

這是因?yàn)閹ぬ柌辉试S從遠(yuǎn)程登陸,只能本機(jī)鏈接。
2、這個(gè)時(shí)候只需在服務(wù)器登入mysql,更改mysql數(shù)據(jù)庫里的user表里的host項(xiàng),從localhost改為%。
mysql> select host from user where user = 'root';
+-----------+
| host |
+-----------+
| localhost |
+-----------+
1 row in set (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
這是就可以連接成功了。
