1.下载mysql5.7版本


[[email protected] home]#wget
http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2.卸载系统自带的Mariadb
Centos7将默认数据库mysql替换成了Mariadb,如果想继续使用mysql 需要卸载Mariadb 再安装mysql
rpm -qa|grep mariadb
rpm -e --nodeps 文件名 (上面指令查出的所有文件) [[email protected] home]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64 [[email protected] home]# rpm -e --nodeps
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
3.删除etc目录下的my.cnf文件


[[email protected] home]# rm -rf /etc/my.cnf4.创建mysql用户组和mysql用户,并加入mysql组

[[email protected] home]# groupadd mysql [[email protected] home]# useradd -r -g
mysql mysql5.将下载的压缩包放到 /usr/local/ 目录下(通过mv 要移动的文件  /usr/local/)

[[email protected] home]# mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /usr/local/
6.进入/usr/local/下,解压安装包

[[email protected] home]# cd /usr/local/ [[email protected] local]# tar -zxvf
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz7. 将解压好的文件夹重命名为mysql

[[email protected] local]# mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql8.在 etc
下新建配置文件my.cnf,并在该文件中添加以下配置代码:

[[email protected] ~]# vi /etc/my.cnf [mysql] # 设置mysql客户端默认字符集
default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld]
skip-name-resolve #设置3306端口 port = 3306 socket=/var/lib/mysql/mysql.sock #
设置mysql的安装目录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放目录
datadir=/home/local/mysql/data # 允许最大连接数 max_connections=500 #
服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf-8 # 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB lower_case_table_names=1 max_allowed_packet=16M


9.创建上面配置文件需要用到的目录,并给用到的目录修改拥有者为mysql用户
[[email protected] local]# mkdir /var/lib/mysql [[email protected] local]# mkdir
/home/local/mysql/data [[email protected] local]# chown -R mysql:mysql
/home/local/mysql/data [[email protected] local]# chown -R mysql:mysql
/var/lib/mysql [[email protected] local]# chown -R mysql:mysql /usr/local/mysql
10.配置环境变量[[email protected] bin]# vi ~/.bash_profile
修改PATH,增加mysql的bin目录:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

11.执行下面的命令是修改的内容立即生效:


[[email protected] bin]# source ~/.bash_profile
12.授予/etc/my.cnf最大权限
[email protected] local]# chown 777 /etc/my.cnf 13.设置开机自启动服务控制脚本:(将{mysql}/
support-files/mysql.server 拷贝为/etc/init.d/mysqld并设置运行权限)

[[email protected] mysql]# cp ./support-files/mysql.server
/etc/rc.d/init.d/mysqld [[email protected] mysql]# chmod +x
/etc/rc.d/init.d/mysqld [[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig --list mysqld 结果显示:
mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关

表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用systemctl 命令控制mysql的启动和停止。
13.初始化数据库

[[email protected] mysql]# /usr/local/mysql/bin/mysqld --initialize-insecure
--user=mysql --basedir=/usr/local/mysql --datadir=/home/local/mysql/data/
14.启动数据库

[[email protected] mysql]# systemctl start mysqld15.初次安装执行安全配置向导

[[email protected] bin]# mysql_secure_installation a)为root用户设置密码
  b)删除匿名账号
  c)取消root用户远程登录
  d)删除test库和对test库的访问权限
  e)刷新授权表使修改生效
具体选项如下:
Securing the MySQL server deployment. Enter password for user root: The
'validate_password' plugin is installed on the server. The subsequent steps
will run with the existing configuration of the plugin. Using existing password
for root. Estimated strength of the password: 100 Change the password for root
? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new
password: Estimated strength of the password: 100 Do you wish to continue with
the password provided?(Press y|Y for Yes, any other key for No) : y 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?
(Press y|Y for Yes, any other key for No) : 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?
(Press y|Y for Yes, any other key for No) : no ... skipping. 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? (Press y|Y for
Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success.
All done!
16.登录mysql,修改远程访问权限
[[email protected] /]# mysql -uroot -pmysql> use mysql; Database changed mysql>
update user set host = '%' where user = 'root'; Query OK, 1 row affected (0.00
sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)