CentOS安装MySQL

CentOS安装MySQL

  • 查看是否安装过mysql
rpm -qa | grep -i mysql
  • 如果有信息出现,则进行删除,删除老版本mysql的开发头文件和库,命令如下:
rpm -e –nodeps 包名
  • 注意:卸载后/var/lib/mysql中的数据及/etc/my.cnf不会删除,如果确定没用后就手工删除,删除完成后,才能进行安装,否则安装会出错。
rm -f /etc/my.cnf
rm -fr /var/lib/mysql
  • 查看旧的mysql安装目录
whereis mysql
  • 删除旧的安装目录
rm -rf 目录
  • 新建目录
mkdir /data

安装MySQL

  • 把MySQL的压缩安装包上传到/usr/local/software目录下
  • 解压
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
  • 重命名
mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
  • 在mysql下面新建目录data
mkdir data
  • 查看mysql用户或组是否存在
id mysql
  • 如果需要删除用户和组,执行下列操作
groupdel mysql
userdel -f mysql
  • 如果删除不成功,则vim /etc/group,找到mysql,并注释掉(#),再次查看是否删除成功
  • 添加mysql用户组和用户,并且将mysql用户和mysql组相关联
  • 创建mysql组
groupadd mysql
# 查看是否创建成功
cat /etc/group | grep mysql
  • 创建mysql用户
useradd -r -g mysql mysql
# 查看是否创建成功
cat /etc/passwd | grep mysql

  • 给mysql用户设置密码为:mysql
passwd mysql
  • 给mysql授权,以便能够读写以及执行必要的文件
chown -R mysql:mysql /usr/local/software/mysql
chmod -R 775 /usr/local/software/mysql
  • 进入mysql/support-files文件夹的内容,查看是否有my-default.cnf配置文件,如果没有默认的配置文件,需要手动创建一个my-default.cnf配置文件
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
basedir = /usr/local/software/mysql
datadir = /usr/local/software/mysql/data
bind-address = 0.0.0.0
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size= 16M
max_connections = 213
wait_timeout = 31536000
interactive_timeout = 30
max_connect_errors = 9
long_query_time = 1
tmp_table_size = 16M
#log-bin=mysql-bin
#binlog_format=mixed
#server-id = 1
lower_case_table_names = 1
[mysqldump]
quick
max_allowed_packet = 8M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 12M
sort_buffer_size = 1M
read_buffer = 1M
write_buffer = 1M
[mysqlhotcopy]
interactive-timeout
  • 上面的填这么多干嘛?搞不明白,而且还有些隐性的错误!!!千万不要用!!!(2020年11月28日19:13:13)
[mysqld]
datadir=/usr/local/software/mysql/data
basedir=/usr/local/software/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-setserver=utf8
# 取消密码验证
skipgranttables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
logerror=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
  • 重新授权
chown -R mysql:mysql /usr/local/software/mysql
chmod -R 775 /usr/local/software/mysql
  • 复制support-files文件下的my-default.cnf和mysql.server,如下:
cp my-default.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysqld
cd /etc/init.d
vim mysqld

  • 配置环境变量
vim /etc/profile
  • 在末尾添加
export MYSQL=/usr/local/software/mysql
export PATH=$PATH:$MYSQL/bin:
  • 使修改后的profile文件生效
source /etc/profile
  • 执行数据库初始化操作
cd /usr/local/software/mysql/bin/
./mysqld –basedir=/usr/local/software/mysql –user=mysql –datadir=/usr/local/software/mysql/data –initialize
  • 如果报error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
# 安装libaio
yum install libaio
  • 如果报:[ERROR] –initialize specified but the data directory has files in it. Aborting,说明已经执行过一次,需要把’/usr/local/software/mysql/data目录删除,重新创建,再授权
cd /usr/local/software/mysql
rm -rf data
mkdir data
chown -R mysql:mysql /usr/local/software/mysql
chmod -R 755 /usr/local/software/mysql
  • 然后再执行
cd /usr/local/software/mysql/bin
./mysqld –basedir=/usr/local/software/mysql –user=mysql –datadir=/usr/local/software/mysql/data –initialize
  • 启动mysql,试试能否启动
service mysqld start
  • 修改密码
service mysqld stop
mysqld_safe –user=mysql –skip-grant-tables –skip-networking &
# 这里不用输密码,直接进入到mysql中
mysql

  • 在mysql中修改密码
mysql> use mysql;
mysql> update user set authentication_string=PASSWORD(‘root’) where user=’root’;
mysql> flush privileges;
mysql> exit;
  • 此时,密码已经修改完成。退出重新登录,本以为搞定了,但是创建数据库,却报错了,错误是:ERROR 1820 (HY000): You must reset your password using ALTER USER statement。原来第一登录,还要再次设置密码,修改之后,创建成功。
# mysql -u root -p
mysql> alter user ‘root’@’localhost’ identified by ‘root’;
mysql> flush privileges;
mysql> exit;
  • 设置远程登录权限
$ vim /etc/my.cnf
# 修改端口为
port=3306
# 重启
$ service mysqld restart
  • 修改远程连接权限
$ mysql -uroot -p
mysql> use mysql;
mysql> update user set host=’%’ where user=’root’;
mysql> exit;
$ service mysqld restart
  • 阿里云开放3306端口
分享到: 生成海报
avatar

热门文章

  • 一对一资讯网 为您提供可靠的服务

    一对一主机系统飞蝌云计算
    登录一对一资讯平台  

    登录

    忘记密码 ?