1、下载MySQL安装包(https://dev.mysql.com/downloads/mysql/)
2、上传mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz文件至/usr/local下
3、解压安装包至/usr/local下
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
4、重命名文件夹为mysql
mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql
5、创建数据文件目录
cd /usr/local/mysql
mkdir data
6、创建用户组和用户
groupadd mysql
useradd -g mysql mysql
7、修改用户权限
chown -R mysql:mysql /usr/local/mysql
8、初始化数据库
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
结尾处为初始密码,将密码复制出来记在记事本上。
若报如下错:
./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object
file: No such file or directory
则执行:
yum remove libnuma.so.1;
yum -y install numactl.x86_64;
9、将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
10、配置my.cnf
搜索my.cnf
find / -name my.cnf
找到后复制到 /etc/my.cnf
并进行如下编辑,若找不到则在/etc下手动编辑一个
[mysqld] character_set_server=utf8 init_connect='SET NAMES utf8' basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock #不区分大小写 lower_case_table_names = 1 #不开启sql严格模式 sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" log-error=/var/log/mysqld.log pid-file=/usr/local/mysql/data/mysqld.pid
11、添加开机自启动
vim /etc/init.d/mysql
编辑其中两个属性如下 :
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
开机自启:
chkconfig --add mysql
chkconfig mysql on
启动mysql:
service mysql stop
service mysql start
查询列表:
chkconfig | grep -i mysql
如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则执行
chkconfig --level 345 mysql on
重启电脑:
reboot
验证:
netstat -na | grep 3306
连接mysql并修改默认密码:
cd /usr/local/mysql/bin ./mysql -uroot -p 然后输入刚才记下来的默认密码,登录后修改默认密码 alter user 'root'@'localhost' identified by '新密码'; flush privileges; ---刷新权限,之后退出重新登录
12、设置远程连接权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;