一:检查旧版本
[root@bogon opt]# rpm -qa | grep mysql
mysql-libs-5.1.73-7.el6.x86_64
[root@bogon opt]#
有的话通过下面的命令来卸载掉
[root@bogon opt]# rpm -e mysql-libs-5.1.73-7.el6.x86_64 //普通删除模式,提示有依赖的其它文件
error: Failed dependencies:
libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
mysql-libs is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
[root@bogon opt]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64 // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
[root@bogon opt]# rpm -qa | grep mysql //再次检查,发现删除完成
[root@bogon opt]#
二:安装MySQL
安装编译代码需要的依赖包:
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
[root@bogon opt]#wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@bogon opt]#tar xvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@bogon opt]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql //移动并改名到需要安装的目录
[root@bogon opt]#
创建mysql用户和组
[root@bogon opt]# groupadd mysql
[root@bogon opt]# useradd -r -g mysql mysql -d /usr/local/mysql
修改目录权限
[root@bogon local]# chown -R mysql:mysql /usr/local/mysql
创建mysql数据存放目录
[root@bogon /]# mkdir -p /data/mysql //当前数据库数据存放在 /data/mysql 下
[root@bogon /]# chown mysql.mysql /data/mysql/ //修改目录权限
安装数据库
[root@bogon /]#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql //#
basedir 安装路径, datadir 数据存放目录
复制mysql配置文件
[root@bogon support-files]# cd /usr/local/mysql/support-files
[root@bogon support-files]# cp my-default.cnf /etc/my.cnf
添加系统服务
[root@bogon support-files]# cp mysql.server /etc/init.d/mysql
[root@bogon support-files]#vim /etc/init.d/mysql
datadir=/data/mysql //修改数据库存放路径
[root@bogon support-files]# chkconfig mysql on //添加开机自启
添加环境变量
#vim /etc/profile //用vim打开profile文件,最后添加下面两条
export MYSQL_HOME="/usr/local/mysql"
export PATH="$PATH:$MYSQL_HOME/bin"
#source /etc/profile // 重新加载配置
修改配置文件
[root@bogon ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
port=3306
server_id=1
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
#eskip-grant-tables
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
启动服务
[root@bogon mysql]# service mysql start
Starting MySQL.Logging to '/data/mysql/bogon.err'. //如果这里报错, 检查var/lib/目录下有没有mysql 文件夹,没有的话创建
180703 06:16:40 mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/data/mysql/bogon.pid).
[root@bogon mysql]# mkdir /var/lib/mysql
[root@bogon mysql]# chown mysql.mysql /var/lib/mysql/
[root@bogon mysql]# service mysql start
Starting MySQL... SUCCESS!
PS:最好把mysql.sock做一个软连接指定到tmp目录,防止后期其他问题
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
设置root密码
[root@bogon mysql]#mysqladmin -u root password '123456'
Warning: Using a password on the command line interface can be insecure.
删除数据库空用户
[root@bogon ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> SELECT user,host,password FROM mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | bogon | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | bogon | |
+------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> delete from mysql.user where user='';
Query OK, 2 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
添加授权用户(可选)
添加新的授权用户:
create user "用户名"@"IP地址" identified by "密码";
示例: create user "mysql"@"%" identified by "mysql"; #添加一个用户名为mysql 密码为mysql 的用户。
IP地址的表示方式:
1)% 所有的IP,表示用户可以从任何地址连接到服务器。
2)localhost 用户只能从本地连接。
3)指定一个IP ,表示用户只能从此IP 连接到服务器。
给指定用户授权(可选)
语法: grant 权限列表 on 库.表 to "用户名"@"IP地址" with grant option;
示例: grant all privileges on *.* to "mysql"@"%" with grant option; ##给mysqly用户授权。所有权限,所有库表可以访问
grant select privileges on my_db.* to "mysql"@"%" with grant option; #给mysqly用户授权。只能对my_db 这个库操作,只有查询权限
grant select privileges on indexdb.* to "mysql"@"%" with grant option; ##给mysqly用户授权。只能对indexdb 这个库操作,只有查询权限
PS :一条语句不能同时添加多个库,如果需要对对个库授权,只能执行多次授权语句。
权限列表:
select, update, delete, insert, alter, drop, create, .....
库.表, *.* 表示所有库的所有表。
登录授权过的用户: mysql -hIP -u用户名 -p
删除授权用户
drop user "用户名"@"iP地址";