1、Centos7 系统自带MariaDB安装MySQL前需要卸载
# 查看系统自带的MariaDB
[root@chensir ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
# 卸载系统自带的MariaDB
[root@chensir ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
# 删除 /etc/my.cnf配置文件
[root@chensir ~]# rm -rf /etc/my.cnf
2、检查MySQL是否存在:rpm -qa |grep mysql
3、创建mysql用户和用户组:adduser mysql
修改mysql用户密码:passwd mysql
4、下载mysql的tar包 下载地址
5、上传下载的MySQL tar包至/usr/local/
6、解压mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
:tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
7、重命名:mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql
8、在/etc
目录下创建my.cnf文件:vim /etc/my.cnf
内容如下:
my.cnf配置内容如下:
[mysqld]
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
# 开启binlog
server-id = 1
log-bin=/usr/local/mysql/log/mysql-bin
explicit_defaults_for_timestamp=true
pid-file = /usr/local/mysql/mysql.pid
log_error = /usr/local/mysql/log/error.log
# 跳过mysql密码验证
# skip-grant-tables
# 解决mysql版本导致的sql_model=only_full_group_by问题
# sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysql.server]
user=mysql
9、创建对应目录:
# 进入mysql目录内
cd /usr/local/mysql
# 新建log文件夹
makir log
# 新建data文件夹
mkdir data
10、更改mysql所属用户为mysql:
chown mysql:mysql -R /usr/local/mysql
11、安装mysql:
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
12、拷贝软连接:cp ./support-files/mysql.server /etc/init.d/mysqld
13、赋可执行权限chmod +x /etc/init.d/mysqld
14、启动mysql:service mysqld start
15、设置开机启动:
chkconfig --level 35 mysqld on
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld status
16、修改配置文件vim /etc/profile
# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
使其生效:source /etc/profile
17、获取数据库初始密码:
cat /root/.mysql_secret
18、修改密码:
mysql -uroot -p
set PASSWORD = PASSWORD('123456');
flush privileges;
19、添加远程访问权限
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> update user set host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
————————————————
flush privileges;
生效
查看是否开启binlog:show variables like '%log_bin%';