[root@localhost home]#wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2.卸载系统自带的Mariadb
[root@localhost home]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@localhost home]# rpm -e --nodeps mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost home]# rm -rf /etc/my.cnf
4.创建mysql用户组和mysql用户,并加入mysql组
[root@localhost home]# groupadd mysql
[root@localhost home]# useradd -r -g mysql mysql
5.将下载的压缩包放到 /usr/local/ 目录下(通过mv 要移动的文件 /usr/local/)
[root@localhost home]# mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /usr/local/
6.进入/usr/local/下,解压安装包
[root@localhost home]# cd /usr/local/
[root@localhost local]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
7. 将解压好的文件夹重命名为mysql
[root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
8.在 etc 下新建配置文件my.cnf,并在该文件中添加以下配置代码:
[root@localhost ~]# 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
[root@localhost local]# mkdir /var/lib/mysql
[root@localhost local]# mkdir /home/local/mysql/data
[root@localhost local]# chown -R mysql:mysql /home/local/mysql/data
[root@localhost local]# chown -R mysql:mysql /var/lib/mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql
[root@localhost bin]# vi ~/.bash_profile
修改PATH,增加mysql的bin目录:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
[root@localhost bin]# source ~/.bash_profile
root@localhost local]# chown 777 /etc/my.cnf
[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --list mysqld
结果显示:
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用systemctl 命令控制mysql的启动和停止。
13.初始化数据库[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/local/mysql/data/
14.启动数据库
[root@localhost mysql]# systemctl start mysqld
15.初次安装执行安全配置向导
[root@localhost bin]# mysql_secure_installation
a)为root用户设置密码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!
[root@localhost /]# mysql -uroot -p
mysql> 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)