安装MySQL
本次安装使用的是绿色硬盘版本,无需额外安装依赖环境,比较简单
cd /opt
tar -xf mysql安装包
mv 解压出的目录 /usr/local/mysql
#创建程序用户
useradd -M -s /sbin/nologin mysql
#mysql的主配置文件设定所属用户和组
chown -R mysql.mysql /usr/local/mysql/
chown mysql.mysql /etc/my.cnf
修改配置文件:
vim /etc/my.cnf
[client]
port = 3306
#客户端访问的端口
socket=/usr/local/mysql/mysql.sock
#指定MySQL的通信套接字文件[mysqld]
user = mysql
basedir=/usr/local/mysql
#MySQL的安装目录
datadir=/usr/local/mysql/data
#MySQL数据保存的目录
port = 3306
#服务端的端口
character-set-server=utf8
#字符集的编码
pid-file = /usr/local/mysql/mysqld.pid
#pid文件位置
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
#表示任意地址都可以访问数据库
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
#MySQL默认的存储引擎
max_allowed_packet=16M
server-id = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
#MySQL支持的数据类型和相关的模块
设置环境变量,声明/宣告MySQL命令便于系统识别:
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
初始化数据库:
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
设置系统识别,进行操作:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl daemon-reload
systemctl restart mysqld
初始化数据库密码:
mysqladmin -u root -p password "123456"
#在交互界面直接回车,不要输入!
进入数据库:
mysql -u root -p123456
#进入数据库交互界面
show databases;
#创建用户并设置密码
create user 'root'@"%" identified by '123456';
#赋予远程连接的权限
grant all privileges on *.* to 'root'@'%';
#刷新生效
flush privileges;
#修改加密方式,可以进行远程连接
alter user 'root'@'%' identified with mysql_native_password by "123456";
exit;