示例:(安装的位置,,一般就是首层位置有变动)
https://downloads.mysql.com/archives/community/
请选择指定版本下载
Mysql包路径:/opt/package/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
安装(base)路径:/var/lib/mysql/
Data路径:/data/mysql/data
Log路径:/data/mysql/log
My.cnf路径:/data/mysql/my.cnf
Mysql.service路径:/usr/lib/systemd/system/mysql.service
账号:root
密码:gzbwybi@2019
启动:systemctl start mysqld
一.安装:
1.创建mysql用户:
useradd mysql
useradd -M -r -d /dev/null -s /sbin/nologin mysql
2.解压:
sudo tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /var/lib/
sudo mv /var/lib/mysql-5.7.38-linux-glibc2.12-x86_64 /var/lib/mysql
3.创建data,log路径:
sudo mkdir -p /data/mysql/{data,logs}
4.修改属主/属组:
sudo chown -R mysql.mysql /data/mysql/
sudo chown -R mysql.mysql /var/lib/mysql/
5.编写my.cnf
vi /data/mysql/my.cnf
[client]
port=3306
[mysql]
default-character-set=utf8
[mysqld]
server-id=12
log-bin=mysql-bin
# 服务器侦听TCP/IP连接的端口号。
port=3306
# MySQL安装基础目录的路径。
basedir=/var/lib/mysql
# MySQL服务器数据目录的路径。最好将datadir值指定为绝对路径。
datadir=/data/mysql/data
# 服务器默认字符集。如果设置此变量,还应设置collation_server以指定字符集的排序规则。
character-set-server=utf8
# 服务器的默认排序规则。
collation_server=utf8_general_ci
# 表的默认存储引擎。此变量仅为永久表设置存储引擎。
# 要为临时表设置存储引擎,请设置default_tmp_storage_engine系统变量。
default-storage-engine=InnoDB
# 允许的最大同时客户端连接数。(1-100000)
max_connections=1024
# 在max_connect_errors之后,来自主机的连续连接请求在没有成功连接的情况下被中断,服务器将阻止该主机进行进一步的连接。
max_connect_errors=1024
# 默认的身份验证插件。(mysql_native_password,sha256_password,caching_sha2_password)
default_authentication_plugin=caching_sha2_password
# soket文件,本地连接时使用
socket=/var/lib/mysql/mysql.sock
# 建议禁用符号链接以防止各种安全风险
symbolic-links=0
# 错误日志
log-error=/data/mysql/log/mysqld.log
log_timestamps=SYSTEM
# pid文件
pid-file=/data/mysql/mysqld.pid
[mysqld]
binlog_cache_size = 256K
thread_stack = 512K
join_buffer_size = 8192K
max_heap_table_size = 2048M
lower_case_table_names=1
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 1024M
max_allowed_packet = 100G
table_open_cache = 2048
sort_buffer_size = 4096K
net_buffer_length = 4K
read_buffer_size = 4096K
read_rnd_buffer_size = 2048K
myisam_sort_buffer_size = 32M
thread_cache_size = 256
tmp_table_size = 2048M
default_authentication_plugin = mysql_native_password
lower_case_table_names = 1
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
explicit_defaults_for_timestamp = true
max_connections = 1000
max_connect_errors = 100
open_files_limit = 65535
[mysqldump]
quick
max_allowed_packet = 1024M
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 2M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
chown -R mysql.mysql my.cnf
6.初始化:
sudo /var/lib/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize-insecure --basedir=/var/lib/mysql --datadir=/data/mysql/data --user=mysql
./bin/mysqld --defaults-file=/data/mysql/conf/my.cnf --initialize-insecure --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
7.编写mysql.service
vi /lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(5.7.38)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
#对路径进行修改
ExecStart=/var/lib/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf
# Sets open_files_limit
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
8.启动:
systemctl start mysqld
二.修改密码
-
在my.cnf文件中添加
skip-grant-tables
-
重启mysql
systemctl restart mysqld
-
修改密码
/var/lib/mysql/bin/mysql -uroot -p -S /var/lib/mysql/mysql.sock 进入数据库执行: use mysql update user set authentication_string='gxData!wyc.com' where user='root'; set password for root@localhost = password('gzbwybi@2019'); (alter user 'root'@'localhost' identified by 'R00T@mysql') FLUSH PRIVILEGES; CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Ya!wyc.com'; GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; exit
-
注释掉my.cnf中的
#skip-grant-tables
-
重启数据库
systemctl restart mysqld
三. 登录:
/var/lib/mysql/bin/mysql -uroot -pR00T@mysql -S /var/lib/mysql/mysql.sock
注:
这个sock文件在my.cnf文件中定义的
网友评论