普通用户安装MySQL
一、解压
cd soft
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
二、创建目录和移动
mkdir /home/XXX/usr/local
mkdir /home/XXX/usr/local/data
mkdir /home/XXX/usr/local/log
mv mysql-5.7.25-linux-glibc2.12-x86_64 /home/XXX/usr/local/mysql
三、新建配置文件my.cnf; 需要修改端口号;
(如果root用户安装的mysql占用了3306,那么普通用户需要修改端口号;这里端口号“port=3336”。)
cd /home/XXX/usr/local/mysql/
vi my.cnf
+++++++++++++my.cnf++++++++++++++++++++++++
[client]
port=3336
socket=/home/XXX/usr/local/mysql/mysql.sock
[mysqld]
port=3336
#设置mysql的安装目录
basedir=/home/XXX/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/home/XXX/usr/local/data
pid-file=/home/XXX/usr/local/mysql/mysql.pid
socket=/home/XXX/usr/local/mysql/mysql.sock
log_error=/home/XXX/usr/local/log/error.log
collation_server=utf8_general_ci
# 服务端使用的字符集默认为8比特编码的latin1字符集
character_set_server=utf8
log-bin=/home/XXX/usr/local/log/mysql-bin
server-id=100
++++++++++++++++my.cnf++++++++++++++++++++
四、安装/初始化数据库
cd /home/XXX/usr/local/mysql
./bin/mysqld --defaults-file=/home/XXX/usr/local/mysql/my.cnf --initialize --user=XXX --basedir=/home/XXX/usr/local/mysql --datadir=/home/XXX/usr/local/data
五、获取root密码
cat /home/XXX/usr/local/log/error.log |grep root@localhost
六、启动mysql
cd /home/XXX/usr/local/mysql/bin/
./mysqld_safe --defaults-file=/home/XXX/usr/local/mysql/my.cnf --user=XXX &
七、登录mysql
cd /home/XXX/usr/local/mysql/bin/
$ ./mysql -uroot -P3336 -p -S /home/XXX/usr/local/mysql/mysql.sock
八、登录后,执行命令修改root密码(mysql@123) 及远程登录设置 ;
alter user 'root'@'localhost' identified by 'mysql@123';
grant all privileges on *.* to 'root'@'%' identified by 'mysql@123' with grant option; #或指定网段登录
use mysql;
select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
+-----------+---------------+
FLUSH PRIVILEGES;
九、测试远程登录
netstat -ntpl |grep 3336
mysql -u root -h XXX.XXX.XXX.XXX(主机IP) -P 3336 -p
+++++++++++++++++++++++++++++++++++++++++++++
报错:ERROR 2003 (HY000): Can't connect to MySQL server
原因:没有开通防火墙或者关闭防火墙.
十、开通本地防火墙策略(需要root权限)
vi /etc/sysconfig/iptables
添加一条:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3336 -j ACCEPT
重启防火墙
service iptables restart