在CentOS7 中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
1 .下载并安装MySQL官方的 Yum Repository.
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@iZ1h0bodqxwyjnZ /]# cd /etc/yum.repos.d/
[root@iZ1h0bodqxwyjnZ yum.repos.d]# ls
CentOS-Base.repo epel.repo mysql-community.repo mysql-community-source.repo
注:安装完后会产生mysql-community.repo mysql-community-source.repo两个yum创库。
yum install mysql-server
Installed:
mysql-community-libs.x86_64 0:5.6.40-2.el7 mysql-community-server.x86_64 0:5.6.40-2.el7
Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7 mysql-community-client.x86_64 0:5.6.40-2.el7 mysql-community-common.x86_64 0:5.6.40-2.el7
perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBI.x86_64 0:1.627-4.el7
perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 perl-Net-Daemon.noarch 0:0.48-5.el7
perl-PlRPC.noarch 0:0.2020-14.el7
Replaced:
mariadb-libs.x86_64 1:5.5.52-1.el7
Complete!
注:安装完后会替换掉默认数据库。
2 .MySQL数据库设置
[root@iZ1h0bodqxwyjnZ /]# systemctl start mysql #启动数据库
[root@iZ1h0bodqxwyjnZ /]# systemctl status mysql #查看数据库状态
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-06-29 15:41:28 CST; 31s ago
Process: 18890 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 18830 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 18889 (mysqld_safe)
CGroup: /system.slice/mysqld.service
注:active为running时即正常。
[root@iZ1h0bodqxwyjnZ /]# mysql -p #进去数据库,默认是root用户,初始登录不需要密码,回车即可。
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.40 MySQL Community Server (GPL)
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
mysql> SHOW VARIABLES LIKE ‘validate_password%’;
Empty set (0.00 sec)
mysql> set password for ‘root’@‘localhost’ =password(‘password’); #设置本地登录账户
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on . to admin@’%'identified by ‘password’; #设置远程登录账户
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; #刷新权限表
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
3.数据库库迁移及配置
注:由于云服务器的系统盘较小,而数据库默认安装在/var下,则将其迁移至相应的数据盘。
#systemctl stop mysql
mkdir mysql #data下创建mysql目录
#cp -vrfp /var/lib/mysql /data/mysql/ #拷贝目录文件到mysql du -sh /data/mysql 查看文件大小
#cd mysql
#mv mysql mysql1 修改mysql文件名
#cp -vrfp mysql1/* /data/mysql 把mysql1的文件放出来
#rm -rf mysql1 #删除mysql1,现在就已经把文件迁移过来了
#把配置文件my.cnf放到/etc下(提供my.cnf文件)
修改/data/mysql的所有者
#chown -R mysql.mysql /data/mysql
启动数据库
#systemctl start mysql
注:此时本地不能登录的话,将mysql.sock链接至原目录下即可。
修改配置文件
vim /etc/my.cnf
character_set_server = utf8
datadir=/data/mysql
socket=/data/mysql/mysql.sock
skip-name-resolve
symbolic-links=0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
注:其他的相关配置优化自行根据情况解决。