CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL。MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
Linux下安装MariaDB官方文档参见:官网地址
全部删除MySQL/MariaDB
MySQL 已经不再包含在 CentOS 7 的源中,而改用了 MariaDB;
1.使用
rpm -qa | grep MariaDB
搜索 MariaDB 现有的包;
如果存在(删不了的话,就一个一个删),使用
rpm -e --nodeps MariaDB-*
全部删除;
2.使用
rpm -qa | grep mysql
搜索 mysql 现有的包:
如果存在(删除你实际查询出来的mysql结果即可),使用
yum remove mysql mysql-server mysql-libs compat-mysql51
全部删除;
3.开始新的安装, 创建MariaDB.repo文件
vim /etc/yum.repos.d/MariaDB.repo
插入一下内容,第二个我换成了国内源中科大的(有问题可查看,网址),(系统及版本选择(国外官网),选择安装你需要的版本,网址):
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
系统及版本选择,你可以根据这个网址(国内)去选择你想安装centos7 amd64位其他版本号的版本,网址。直接修改baseurl中的版本号即可。安装Mariadb之前,你可以先导入GPG key
rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
4.运行安装命令安装MariaDB(安装过程可能会比较漫长,使用中科大的就快了)
yum -y install MariaDB-server MariaDB-client
5.下载安装包,进行自动安装,安装成功之后启动MariaDB服务,并设为开机自启。
systemctl start mariadb #启动服务
systemctl enable mariadb #设置开机启动
systemctl restart mariadb #重新启动
systemctl stop mariadb.service #停止MariaDB
6.登录到数据库用
mysql -uroot -p
登录到MariaDB,此时root账户的密码为空,直接回车即可,退出Mariadb,exit;即可。
7.进行MariaDB的相关简单配置,使用
mysql_secure_installation
命令进行配置(先退出数据库)。
首先是设置密码,会提示先输入密码
Enter current password for root (enter for none):<–初次运行直接回车
设置密码
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
其他配置
Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车(后面授权配置)
Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车
Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车
初始化MariaDB完成,直接登录,成功。
8.配置MariaDB的字符集
使用vim /etc/my.cnf.d/server.cnf命令编辑server.cnf文件,在[mysqld]标签下添加:
init_connect=‘SET collation_connection = utf8_unicode_ci’
init_connect=‘SET NAMES utf8’
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
配置初始化完成,重启Mariadb。
systemctl restart mariadb
之后进入Mariadb,查看字符集。
show variables like “%character%”;show variables like “%collation%”;
9.添加用户,设置权限
创建用户命令(用户名,密码请自行修改)
create user username@localhost identified by ‘password’;
授予外网登陆权限
grant all privileges on . to username@’%’ identified by ‘password’;
select host,user,password from user;
简单的用户和权限配置就完成了。
授予部分权限只需把all privileges改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分即可。
最后的一些细节整理:
systemctl restart mysql 启动
vi /etc/my.cnf.d/server.cnf
lower_case_table_names=1
collation-server = utf8_general_ci
character-set-server = utf8
transaction-isolation = READ-COMMITTED
key_buffer = 16M
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1
max_connections = 550
#expire_logs_days = 10
#max_binlog_size = 100M
#log_bin should be on a disk with enough free space.
#Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your
#system and chown the specified folder to the mysql user.
log_bin=/var/lib/mysql/mysql_binary_log
#In later versions of MariaDB, if you enable the binary log and do not set
#a server_id, MariaDB will not start. The server_id must be unique within
#the replicating group.
server_id=1
binlog_format = mixed
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
# InnoDB settings
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M
systemctl start mariadb.service 启动
重新安装的时候,一定要将之前的卸载干净:
#卸载数据库:
[root@localhost logs]# yum -y remove mari
#删除数据库文件:
[root@localhost logs]# rm -rf /var/lib/mysql/**