# **步骤:**
## 1. 使用xshell等工具登录至服务器,切换至root用户:
##
```
su
Password:
```
# 说明:开启root登录后,可以使用su命令切换至root,如果你的主机还不能使用su切换到root用户,那就需要初始化一下root用户的密码了,方法如下。
```
sudo passwd root
Enter new UNIX password:
```
设置一个root密码,就可以su到root用户了。
## 2. 使用apt安装MySQL:
##
```
apt-get install mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
mysql-server is already the newest version (5.7.20\-0ubuntu0.16.04.1).
0 upgraded, 0 newly installed, 0 to remove and 185 not upgraded.
```
说明:由于已经安装了最新版本的MySQL,所以会有上边的提示。
注意:如果想要使用apt安装,请确保你的主机可以连接到互联网。此处会提示设置Mysql root用户的密码等,Y/N确认?等,正常输入就好。
## 3. 到此MySQL服务器就安装成功了,下面我们验证一下是否安装成功,并做一些配置,免得产生不必要的麻烦:
查看进程:
```
ps -aux|grep mysql|grep -v grep
mysql 278660.029.71148940263592 ? Ssl Nov14 19:38 /usr/sbin/mysqld
```
查看端口:
```
netstat -nlt|grep 3306
tcp 000.0.0.0:33060.0.0.0:\* LISTEN
```
说明:MySQL默认使用3306端口,查看该端口的状态
查看service状态:
```
service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-11-1422:50:15 CST; 1 months 1 days ago
Main PID: 27866 (mysqld)
Tasks: 35
Memory: 263.1M
CPU: 19min 39.122s
CGroup: /system.slice/mysql.service
└─27866 /usr/sbin/mysqld
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
```
说明:显示如上结果,说明service正常
通过启动命令检查MySQL服务器状态:
```
service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017\-11\-1422:50:15 CST; 1 months 1 days ago
Main PID: 27866 (mysqld)
Tasks: 35
Memory: 263.1M
CPU: 19min 39.122s
CGroup: /system.slice/mysql.service
└─27866 /usr/sbin/mysqld
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
root@VM-202\-164\-ubuntu:/home/ubuntu# /etc/init.d/mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017\-11\-1422:50:15 CST; 1 months 1 days ago
Main PID: 27866 (mysqld)
Tasks: 35
Memory: 263.1M
CPU: 19min 39.177s
CGroup: /system.slice/mysql.service
└─27866 /usr/sbin/mysqld
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
```
说明:显示如上结果,说明MySQL服务器状态正常。
到此MySQL数据库就可以正常使用了,可以试一下:
```
mysql -u root -p
Enter password:
mysql> use mysql;
Database changed
mysql> select * from user;
mysql>exit
Bye
```
## 4. 配置MySQL服务器的charset,此处解决MySQL插入中文乱码的情况。
```
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
#在[mysqld]标签下,增加服务器端的字符编码,增加[client]修改客户端编码
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
```
说明:修改mysqlld.cnf这个配置文件,此处针对MySQL 5.7.20,各个版本的MySQL配置文件结构略有差异。但是需要修改的内容不变。修改完之后需要重启MySQL。
```
/etc/init.d/mysql restart
```
最后,验证一下:
```
1. mysql -u root -p
2. mysql> show databases;
3. mysql> use information\_schema;
4. mysql> show tables;
5. mysql> show variables like '%char%';
```
依次执行上边这个↑,如果结果是下边这个↓,所有字符集都变成了utf-8,就搞定了。
```
1. mysql> show variables like '%char%';
2. +--------------------------+----------------------------+
3. | Variable\_name | Value |
4. +--------------------------+----------------------------+
5. | character\_set\_client | utf8 |
6. | character\_set\_connection | utf8 |
7. | character\_set\_database | utf8 |
8. | character\_set\_filesystem | binary |
9. | character\_set\_results | utf8 |
10. | character\_set\_server | utf8 |
11. | character\_set\_system | utf8 |
12. | character\_sets\_dir | /usr/share/mysql/charsets/ |
13. +--------------------------+----------------------------+
14. 8 rows in set (0.17 sec)
```
## 5. 让MySQL可以被远程访问到。
在MySQL的使用中,我们经常使用可视化界面的软件去连接数据库,比如SQLyog这个软件,但是配置好主机ip、用户名、密码等等等之后发现连接失败了,以下配置就是解决这个问题,开启MySQL服务器的远程访问。
```
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
#修改bind-address127.0.0.1为0.0.0.0
bind-address = 0.0.0.0
mysql -u root -p
Enter password:
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "你的密码"; --ROOT为密码
mysql> flush privileges;
```
授权root用户的远程访问。接下来是重启MySQL:
```
/etc/init.d/mysql restart
```
最后,验证试试,看看还显不显示连接失败。
6. 再给几个常用MySQL命令:
```
1. #MySQL服务器开启,关闭,重启
2. /etc/init.d/mysql start
3. /etc/init.d/mysql stop
4. /etc/init.d/mysql restart
```
7. 在调试java程序的时候抛出一个异常(具体是什么不记得了),反正有max_allowed_packet这个字段的提示,这是由于insert或者update语句中包含长文本,太长了超过了某个阈值(MySQL为保证IO的正常而设置的阈值),而这个阈值是在MySQL服务端设置的,不管你有没有遇到这个异常呢,先记下来吧,以备不时之需。其实设置步骤很简单:
```
mysql -u root -p
Enter password:
mysql> show VARIABLES like '%max_allowed_packet%';
mysql> set global max_allowed_packet = 2*1024*1024*10;
```
max_allowed_packet单位是b,所以2\*1024\*1024\*10代表20M。然后是重启,这个不重启是不生效的。
```
/etc/init.d/mysql restart
```
最后验证一下:
```
mysql -u root -p
Enter password:
mysql> show VARIABLES like '%max_allowed_packet%';
```
- 第一章Java环境安装
- 1.Jdk环境安装
- 1.1Windows
- 1.2Linux
- 1.3MaxX
- 2.Tomcat环境
- 3.Git环境配置
- 3.1Git_Windows
- 3.2Git_Linux
- 3.3Git基本操作命令
- 4.SVN配置
- 4.1TortoiseSVN_Windows
- 5.maven安装
- 5.1Maven_Windows
- 6.RabbitMQ安装
- 6.1RabbitMQ_Windows
- 6.2RabbitMQ_CentOS
- 6.3RabbitMQ_MacOS
- 7.mysql安装
- 7.1Mysql_Windows
- 7.2Mysql_CentOS
- 7.3 Mysql_Ubuntu
- 7.4Mysql_MacOS
- 8.Redis安装
- 8.1Redis_windows
- 8.2Redis Desktop Manage
- 8.3Redis_linux&macOS
- 9.zookeeper安装
- 9.1Zookeeper_Windows
- 12.kafka安装
- 10.nigix安装
- 1.1MacX
- 1.2Linux
- 11.docker安装
- 第二章Python环境
- 1.Python安装
- 2.conda环境安装
- 第三章Rasa环境安装
- 1.MITIE安装
- 第四章VUE环境安装
- 1.Node环境安装