云服务器:阿里云CentOS7.3 64
MySQL的安装会因为版本的不同而出现不同的安装和设置方式,以下以MySQL5.7版本做演示,应该算是比较简单的安装方式了,按照步骤来不会出现各种坑。
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为当前启动的,如果想启动其他禁止的,可使用一下命令
[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、修改默认密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。(mysql5.6密码为空直接可以进入,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
使用默认密码登录进去并修改mysql密码,并刷新权限(mysql5.7对密码复杂度会进行校验。必须含有数字,小写或大写字母,特殊字符;长度要不小于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)
如果需要设置诸如123456之类的简单密码,也是有办法可行的。
首先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
validate_password_length(密码长度)参数默认为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、远程连接mysql
1、mysql默认的端口是3306,一般新服务器是没有对此端口开放的,所以需要自行上云提供商后台配置安全组规则,开放3306端口。
开放后会发现连接还是不行,报错如下图所示:
这是因为帐号不允许从远程登陆,只能本机链接。
2、这个时候只需在服务器登入mysql,更改mysql数据库里的user表里的host项,从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)
这是就可以连接成功了。