Up in the Air!
在云端
Home
Categories
Archives
Tags
About
Home
在Centos 6和7上安装设置MySQL / MariaDB 5.5或以上的版本
在Centos 6和7上安装设置MySQL / MariaDB 5.5或以上的版本
取消
在Centos 6和7上安装设置MySQL / MariaDB 5.5或以上的版本
由
aoeII
发布于 2015-08-18
·
最后更新:2018-09-04
1234
本文介绍在Centos 6和7上如何安装设置MySQL / MariaDB 5.5或以上的版本。 欢迎转载,转载请注明出处:https://aoeii.com/posts/how_to_install_mysql_on_centos_6_and_7 ---------- # 准备工作 ## 安装Centos的epel源 Centos 6 yum install -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 Centos 7 yum install -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 ## 安装有Mysql 5.5的源 Centos 6 yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi cp /etc/yum.repos.d/remi.repo /etc/yum.repos.d/remi.repo.`date +%Y%m%d%H%M%S`.bak num=$(awk '/enabled=0/{print NR}' /etc/yum.repos.d/remi.repo | sed 'q') && sed -i ''$num'c enabled=1' /etc/yum.repos.d/remi.repo Centos 7 > CentOS 7 的base/updates repo中已经包含MariaDB 5.5,无需再添加其他源 ### 显示是否生效 yum repolist all | grep remi ### 更新组件并安装必要的软件 yum makecache && yum update -y && yum -y install wget vim zip unzip gzip xz bzip2 l*zip p7zip ---------- ## 安装MySQL 5.5 Centos 6 yum install -y mysql mysql-server --enablerepo=remi Centos 7 yum install -y mariadb mariadb-server ### 设置自动启动并启动服务 Centos 6 chkconfig mysqld on && service mysqld start Centos 7 查看服务汇总 systemctl list-unit-files --type service 启动服务并设置服务自动启动 systemctl start mariadb.service && systemctl enable mariadb.service ### 拷贝配置文件(可选) cp /usr/share/mysql/my-medium.cnf /etc/my.cnf sed -i 's/log-bin=mysql-bin/#log-bin=mysql-bin/g' /etc/my.cnf # 设置不需要记录二进制log ### 为root账户设置密码 mysql_secure_installation 返回信息 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): 因为初始安装,还没有设置密码,直接回车, OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] 输入Y,输入2次密码(输入密码时屏幕不显示字符),回车 Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] 为提高安全性,删除匿名用户,输入Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] 为提高安全性,禁止root用户远程登录,输入Y ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] 移除测试数据,输入Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] 重新载入权限表,输入Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! 至此,安装完成。 ### 重新启动 MySQL Centos 6 service mysqld restart Centos 7 systemctl restart mariadb.service ---------- # 安装脚本汇总 Centos 6 #!/bin/sh yum install -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi cp /etc/yum.repos.d/remi.repo /etc/yum.repos.d/remi.repo.`date +%Y%m%d%H%M%S`.bak num=$(awk '/enabled=0/{print NR}' /etc/yum.repos.d/remi.repo | sed 'q') && sed -i ''$num'c enabled=1' /etc/yum.repos.d/remi.repo yum makecache && yum update -y && yum install -y wget vim zip unzip gzip xz bzip2 l*zip p7zip yum install -y mysql mysql-server --enablerepo=remi chkconfig mysqld on service mysqld start mysql_secure_installation ---------- Centos 7 #!/bin/sh yum install -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 yum makecache && yum update -y && yum install -y wget vim zip unzip gzip xz bzip2 l*zip p7zip yum install -y mariadb mariadb-server systemctl enable mariadb.service systemctl start mariadb.service mysql_secure_installation ---------- # CentOS使用 MariaDB 官方软件源安装 MariaDB的其他高版本 1)、添加MariaDB官方软件源,进入[MariaDB 官方软件源地址](https://downloads.mariadb.org/mariadb/repositories/#mirror=nethub&distro=CentOS)选择相应的CentOS和MariaDB版本。 2)、在`/etc/yum.repos.d/`文件夹中添加一个文件`mariadb.repo`,添加选择相应MariaDB版本后的YUM软件源信息,如CentOS 7 x64的MariaDB 10.1的官方软件源内容为: # MariaDB 10.1 CentOS repository list # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 3)、安装MariaDB数据库服务器 yum install MariaDB-server MariaDB-client 4)、运行`mysql_secure_installation`脚本设置安全项 ------------ # CentOS使用 Mysql 官方软件源安装 Mysql的其他高版本 ## 添加或安装Mysql社区提供的源 ### 方法1 vim /etc/yum.repos.d/mysql-community.repo 输入内容保存 # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 注意:因为5.6和5.7只能安装其中之一,所以其中一个的 enabled = 1,另一个的 enabled = 0 ### 方法2 CentOS 6 rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql CentOS 7 rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 里面包含从5.5 5.6 5.7和 8.0的源,默认启用的是5.7,安装其他版本请更改对应[mysql-community]下的enabled = 0 为 1,且互相不能冲突。 ## 检查源是否生效 yum repolist all | grep mysql ## 开始安装 yum install -y mysql-community-server ## 设置开机启动并启动服务 Centos 6 chkconfig mysqld on service mysqld start Centos 7 systemctl enable mysqld.service systemctl start mysqld.service ## 为root账户设置密码 mysql_secure_installation ------ # 常用命令 ## 设置 mysql 密码 mysqladmin -uroot password 123456 ## 允许远程访问 mysql安装后默认是只能本地`localhost`访问,如果mysql服务准备提供给除本机以外的服务器使用,如果打算从外部访问需要将访问限制改为`%`,意味着所有ip均可以访问 进入mysql命令行模式 mysql -u root -p 输入root用户的密码(输入结果不会显示出来)回车 返回 > Welcome to the MariaDB monitor. Commands end with ; or \g. > Your MariaDB connection id is 3 > Server version: 5.5.60-MariaDB MariaDB Server > > Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. > > Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. > > MariaDB [(none)]> 并输入以下命令 > use mysql; # 此操作会有错误提示, 可不理会 > update user set host = '%' where user= 'root'; # 查看修改结果 > select host, userfrom user; # 可将localhost和127.0.0.1的记录删除 (当前你也可以授权他们) > delete from userwhereuser= '127.0.0.1';mysql> delete from userwhereuser= 'localhost'; # 刷新权限表(此操作一定要执行, 否则需要重启mysql) > flush privileges; 或者 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的root用户密码' WITH GRANT OPTION; # 刷新权限 flush privileges; 以上是对root用户的操作,一般来说风险较大,不建议直接开放root用户的远程访问权限。 **建议只开放特定数据库用户的外部访问权限**: 例如,只允许用户jack从ip为10.10.50.127的主机连接到mysql服务器,并使用654321作为密码 GRANT ALL PRIVILEGES ON *.* TO 'jack'@'10.10.50.127' IDENTIFIED BY '654321' WITH GRANT OPTION; # 刷新权限 flush privileges; 或者,允许用户jack从所有ip连接到mysql服务器,并使用654321作为密码 GRANT ALL PRIVILEGES ON *.* TO 'jack'@'%' IDENTIFIED BY '654321' WITH GRANT OPTION; # 刷新权限 flush privileges; 如果开启了防火墙, 需要放开数据库对外端口, 一般为3306端口: Centos 6 iptables -I INPUT -p tcp --dport 3306 -j ACCEPT service iptables save service iptables restart Centos 7 firewall-cmd --list-all-zones #查看所有的zone信息 firewall-cmd --get-default-zone #查看默认zone是哪一个 firewall-cmd --get-active-zone #查看活动zone是哪一个 firewall-cmd --zone=public --add-port=3306/tcp --permanent #为public zone永久打开3306端口 firewall-cmd --reload #重新加载配置 ---------- ## 重置root用户密码 此操作分两种情况 1. 记得 root 用户密码 其中 123456 为原密码, 不能与-p分开, abcdefg 为新密码 mysqladmin -uroot -p123456 password abcdef 2. 忘记 root 用户密码 如果 MySQL 正在运行, 先关闭 ``` killall -TERM mysqld ``` 再启动MySQL安全模式 mysqld_safe --skip-grant-tables & 此时就是免密进入MySQL > use mysql; > update user set password=password("new_pass") where user="root"; > flush privileges; 退出, 正常启动 MySQL 。 ------ # 让 MySQL 支持4字节 Unicode 编码 utf8mb4 修改配置文件,启用`InnoDB` settings vim /etc/my.cnf 添加以下内容并保存 [mysqld] innodb_large_prefix=1 innodb_file_format=barracuda innodb_file_per_table=1 重启 MySQL 服务 service mysqld restart # Centos6 systemctl restart mariadb # Centos7 改变现有指定数据库`your_databasename`的字符集设置 ALTER DATABASE your_databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ---------- # Mysql修改数据文件默认目录datadir mysql 数据库的数据文件默认安装的路径在`/var/lib/mysql`,但是实际中,我们在安装时常常就需要定制安装数据库路径,比如`/data/mysql`,或者`/opt/mysql`,通常是一个单独的一块盘或者分区,这样有利于性能调优和保护数据安全,同时也方便进行维护。 步骤: 1. 关闭Selinux 临时关闭 setenforce 0 永久关闭(需要重启) vim /etc/selinux/config 将 SELINUX=enforcing 改为 SELINUX=disabled 2. 停止MySQL: service mysql stop # CentOS 6 systemctl stop mariadb.service # CentOS 7 3. 改变数据存储目录: // 新建文件夹用于存放数据,并把mysql默认的数据文件拷贝至此文件夹 mkdir -p /data cp -rap /var/lib/mysql /data 或者 mv /var/lib/mysql /data // 如果提示`Permission denied`,可能需要用mysql用户执行这个命令 runuser -l mysql -c 'cp -rap /var/lib/mysql /data' //如果很不幸,提示用户不可用 This account is currently not available. 需要改一下mysql用户的登录模式 usermod -s /bin/bash mysql 执行完命令后别忘了改回去 usermod -s /sbin/nologin mysql // 改变新建文件夹的属主 chown -R -v mysql:mysql /data/mysql // 现在编辑MySQL的默认配置文件/etc/my.cnf 修改datadir和socketvariable。 vi /usr/my.cnf Change From: datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock Change To: datadir=/data/mysql socket=/data/mysql/mysql.sock 4. 为mysql.sock加软连接: // 比如说实际的mysql.sock在 /data/mysql/mysql.sock 下 ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock 5. 启动 MySQL: service mysql start # CentOS 6 systemctl start mariadb.service # CentOS 7 6. Q&A: Q:以下是修改mysql数据目录会出现的错误: `ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’` ,本例是将Mysql的数据目录移动到了`/data/`下,即mysql.sock所在的真实目录为`/data/mysql/mysql.sock`,mysql有tcp连接和socket连接方式,而上面这种错误一般是因为mysql是使用rpm方式安装的,它会自动寻找 `/var/lib/mysql/mysql.sock` 这个文件,是一种socket连接方式。 A:常见解决办法如下: 1. 创建/修改文件 `/etc/my.cnf`文件,在[mysqld]上面添加: [client] // password=your_password port=3306 // 在这里写上你的mysql.sock的正确位置 socket=/data/mysql/mysql.sock 2. 指定IP地址,使用tcp方式连接mysql,而不使用本地socket方式: mysql -h127.0.0.1 -uuser -ppassword 3. 为 mysql.sock 加个软连接,比如说实际的mysql.sock在 /data/mysql/ 下,则: ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock
blog
linux
mysql
centos
linux
该博客文章由作者通过
CC BY 4.0
进行授权。
分享
最近更新
ESXi 6.7 离线升级 7.0U3
N5105 软路由安装 ESXi 7 直通核显给 Debian / Ubuntu 虚拟机通过 Docker 实现 jellyfin 硬件转码视频文件(硬解/编码)
Leanote 支持 emoji、chart.js 以及 mermaid
手把手教你自建开源免费私有云盘服务nextcloud 14
在Centos 6和7上安装设置MySQL / MariaDB 5.5或以上的版本
热门标签
linux
centos
python
ESXi
windows
leanote
mysql
font
Docker
phpmyadmin
文章目录
在Centos 6和7上安装设置nginx 1.1x
在Centos 6和7上安装设置phpMyAdmin 4.8