作者:Eddy  历史版本:1  最后编辑:Eddy  更新时间:2024-11-15 09:05

MySQL 5.6+
以下操作使用root用户

下载方式一:

mysql-5.7.28-linux下载

下载方式二:

$ wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar

解压:

$ 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

创建mysql用户组/用户

$ groupadd mysql
$ useradd mysql -g mysql
$ passwd mysql #键入mysql作为该用户的密码
$ chown -R mysql:mysql /usr/local/mysql

编辑my.cnf配置文件

$ vi /etc/my.cnf

文件内容为:

[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保存并退出
注意不要遗漏数据库格式为utf8

创建相关文件并授权:

$ 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

初始化数据库

$ cd /usr/local/mysql/bin/
$ ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

设置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

查看默认密码

$ 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;