centos7默认不支持mysql,默认支持的是mariadb,mariadb是mysql一个开源分支卸载mariadb,否则安装mysql会出现冲突rpm -qa | grep mariadb列出所有被安装的mariadb rpm 包;执行命令rpm -e --nodeps 包名称先检查是否存在已安装的MySQLyum list installed | grep mysql若存在删除:yum remove 软件名称yum repolist enabled | grep "mysql.*-community.*"可以看到可能没有5.6的版本centos自带的repo是不会自动更新每个软件的最新版本,所以无法通过yum方式安装MySQL的高级版本添加官方的yum源创建并编辑mysql-community.repo文件vim /etc/yum.repos.d/mysql-community.repo将以下内容粘贴进去并保存[mysql56-community]name=MySQL 5.6 Community Serverbaseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/enabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql同理,其他centos版本安装其他版本的mysql只需要改为对应的baseurl即可,比如centos7安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/centos6安装mysql5.6:baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/centos6安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/这个时候再查看 出现了5.6yum repolist enabled | grep "mysql.*-community.*"执行安装yum -y install mysql-community-server安装完mysql server时,一般要执行这个脚本对数据库初始化。 运行这个脚本,会初始化mysql的data目录,并且创建那些系统表。 同样也会初始化系统表空间并且关联innodb表与数据结构。找到mysql_install_db目录find / -name mysql_install_db显示/usr/bin/mysql_install_db然后执行/usr/bin/mysql_install_db --user=mysql加入开机启动systemctl enable mysqld启动mysqlsystemctl start mysqld查看mysql状态systemctl status mysqld停止mysqlsystemctl stop mysqld重启mysqlsystemctl restart mysqld 执行mysql_secure_installation来做一些常规化安全设置 /usr/bin/mysql_secure_installation提示输入密码,没有密码就直接回车提示设置root user密码 Y生产环境建议删除系统创建的匿名用户 Y禁止root用户远程登录 Y删除test数据库 Y重载权限表 Y初次安装mysql的root账户是没有密码的 登录修改密码mysql -u rootset password for 'root'@'localhost' = password('123456');flush privileges;mysql主配置文件相关配置样例vim /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockcharacter_set_server = utf8sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid[mysql]socket=/var/lib/mysql/mysql.sockdefault-character-set = utf8[client]socket=/var/lib/mysql/mysql.sock重启mysqlsystemctl restart mysqld 开启远程连接:查看目前开放的端口firewall-cmd --zone=public --list-ports增加开放3306端口firewall-cmd --zone=public --add-port=3306/tcp --permanent重启防火墙firewall-cmd --reload增加一个远程账号grant all privileges on *.* to 'zns'@'%' identified by 'zns123456' with grant option;可能出现 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables opt执行flush privileges;重启mysqlsystemctl restart mysqld 卸载mysql查看已安装的mysqlrpm -qa | grep -i mysql执行卸载yum remove mysql-community-libs mysql-community-client mysql-community-server mysql-community-common