以 mysql 5.7+为例,操作使用用户:root
下载包名为:mysql-5.7.28-linux-glibc2.12-x86_64.tar
1、下载安装包
下载地址移步官网:https://downloads.mysql.com/archives/community 选择linux如下图:
2、Linux服务器操作
使用xsehll上传,将包放入Linux的目录,比如:/usr/local/mysql
2.1、解压安装包
$ tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar
$ mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
$ cd /usr/local/mysql
$ mkdir data
2.2、创建用户组及用户
保持在:cd /usr/local/mysql目录
$ groupadd mysql
$ useradd mysql -g mysql
$ passwd mysql #键入mysql作为该用户的密码
$ chown -R mysql:mysql /usr/local/mysql #授权用户
2.3、编辑 my.cnf文件
$ vi /etc/my.cnf
按大写的i进入修改模块,将下面代码复制进去
[client]
default-character-set = utf8
[mysqld]
port = 3306
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci
max_connections = 1024 # 最大连接数量
max_allowed_packet = 64M
lower_case_table_names = 1 # 关闭大小写敏感
innodb_buffer_pool_size = 256M
join_buffer_size = 256M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/var/run/mysqld/mysql.sock
symbolic-links=0
general_log=on
general_log_file=/var/log/mysqld.general.log
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
按:X
保存并退出
2.4、创建相关文件并授权
$ touch /var/run/mysqld/mysql.sock
$ chown mysql:mysql /var/run/mysqld/mysql.sock
$ chmod 755 /var/run/mysqld/mysql.sock
$ touch /var/log/mysqld.log
$ chown -R mysql:mysql /var/log/mysqld.log
$ chmod 755 /var/log/mysqld.log
$ touch /var/log/mysqld.general.log
$ chown -R mysql:mysql /var/log/mysqld.general.log
$ chmod 755 /var/log/mysqld.general.log
$ mkdir -p /var/run/mysqld
$ touch /var/run/mysqld/mysqld.pid
$ chown -R mysql:mysql /var/run/mysqld
$ chmod 755 /var/run/mysqld/mysqld.pid
2.5、初始化数据库
$ cd /usr/local/mysql/bin/
$ ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2.6、设置mysql服务
$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig --level 2345 mysqld on
添加环境变量
$ vi /etc/profile
追加内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
编译环境配置
$ source /etc/profile
启动服务
service mysqld start
验证是否启动成功
service mysqld status
2.7、密码修改
查看默认密码
$ cat /var/log/mysqld.log | grep password
# ay9hBeX<<5Kh
修改密码和开放远程链接
$ mysql -u root -p
# ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
$ ln -s /var/run/mysqld/mysql.sock /tmp/mysql.sock
$ mysql -u root -ppassword # -pay9hBeX<<5Kh ay9hBeX<<5Kh是默认密码
mysql> show databases;
mysql> set password = password('123456'); # 修改密码
mysql> use mysql;
mysql> update user set host = '%' where user = 'root'; # 开放远程连接
mysql> flush privileges;
至此,mysql安装成功!
作者:黄春艳 创建时间:2024-01-03 09:54
最后编辑:黄春艳 更新时间:2024-11-15 11:25
最后编辑:黄春艳 更新时间:2024-11-15 11:25