MySQL安装
本文在CentOS7虚拟机下用rpm的方式安装mysql,版本为64位5.5.48
mysql安装包下载地址: https://downloads.mysql.com/archives/community/
一、安装
#### 1、将安装包上传至 /opt目录下面,解压 ``` tar -xvf MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar ``` ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327105511004-972864101.png)2、卸载centos7自带的mariadb,避免安装MySQL时引起冲突
rpm -qa | grep mariadb
rpm -e mariadb-libs-5.5.35-3.el7.x86_64 --nodeps
3、安装服务端和客户端
rpm -ivh MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
rpm -ivh MySQL-client-5.5.48-1.linux2.6.x86_64.rpm
二、验证MySQL是否安装成功
#### 1、查看是否有mysql用户和用户组 ``` cat /etc/passwd | grep mysql ``` ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327112939195-2124064198.png)cat /etc/group | grep mysql
2、启动服务
service mysql start
3、查看mysql进程
ps -ef | grep mysql
4、查看mysql端口
netstat -tunpl | grep 3306
三、设置root用户密码
``` /usr/bin/mysqladmin -u root password 'root' ``` #### 登录mysql ``` mysql -u root -p ``` ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327113625868-544207990.png)四、更改字符集为utf-8
#### 1、查看字符集 ``` show variables like '%char%'; ``` ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327121713653-418103382.png) 可以看到character_set_database和character_set_server默认都是latin1,这样会引起中文乱码 #### 2、修改字符集 将/usr/share/mysql路径下的my-huge.cnf配置文件复制到 /etc下面,并命名为my.cnf ``` cp /usr/share/mysql/my-huge.cnf /etc/my.cnf ``` 再修改/etc/my.cnf文件 在[client]下面添加一行: ``` default-character-set=utf8 ``` ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327142453996-2041109014.png)在[mysqld]下面添加一行:
character-set-server=utf8
在[mysql]下面添加一行:
default-character-set=utf8
重启MySQL之后可以看到字符集变成utf8了
show variables like '%char%';
五、用工具连接MySQL
连接报错,not allowed to connect to this MySQL server: ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190327142913092-2099881703.png)这个错误其实就是MySQL对root用户做了连接限制
show databases;
use mysql;
select host,user from user;
可以看到,root用户被限制了只能本机连接,所以外部IP无法连接。
修改这个限制,有报错不需要管:
update user set host = '%' where user = 'root';
再刷新一下权限就OK了
FLUSH PRIVILEGES;