【安装】Mysql在Linux上安装

时间:2023-03-08 17:25:38

1、下载

下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads

下载版本:mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz(通用版,linux下64位)

系统:CentOS6.5

也可以直接复制64位的下载地址,通过命令下载:wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz

版本检查是否安装

[root@web1 ~]# rpm -qa|grep mysql
mysql-libs--.el6.x86_64

卸载 --nodeps 表示强制删除,否则会出现组件被其他程序依赖或者占用的情况

[root@web1 ~]# rpm -e mysql-libs--.el6.x86_64 --nodeps
[root@web1 ~]# rpm -qa|grep mysql

如果是编译安装的方式可以通过命令删除所有mysql相关的文件和文件夹即可

find / -name mysql

注意:卸载后/etc/my.cnf不会删除,需要进行手工删除

rm -rf /etc/my.cnf
2.解压安装包
tar -zxvf  并mv到/opt/module下,移动后的文件夹名称为mysql
tar -zxvf mysql--linux-glibc2.-x86_64.tar.gz -C /opt/module
cd /opt/module
mv  mysql--linux-glibc2.-x86_64 mysql

进入mysql文件夹目录

cd mysql
3.创建用户组和用户
先检查是否存在mysql用户和用户组
[root@node1 mysql]# groups mysql
mysql : mysql

不存在则添加

groupadd mysql useradd -r -g mysql mysql
4.修改权限
在data目录下创建文件夹mysql
mkdir data/mysql
进入mysql目录修改权限
chown -R mysql:mysql ./
5.安装数据库
./scripts/mysql_install_db --user=mysql --datadir=/opt/module/mysql/data/mysql

如果报错:

Installing MySQL system tables..../bin/mysqld: error : cannot open shared object file: No such file or directory

那么需要安装numactl.x86_64

yum install numactl.x86_64
6.添加mysql为系统服务
cp support-files/mysql.server /etc/init.d/mysqld
chmod  /etc/init.d/mysqld
cp support-files/my-default.cnf /etc/my.cnf
7.修改mysql服务路径配置
vim /etc/init.d/mysqld
basedir=/opt/module/mysql/
datadir=/opt/module/mysql/data/mysql
8.添加mysql系统变量
vim ~/.bash_profile
MYSQL_HOME="/opt/module/mysql"
export PATH=$PATH:$MYSQL_HOME/bin
#使环境变量生效source ~/.bash_profile
#查看是否加入成功echo $PATH
9.启动mysql
service mysqld start  
10.命令行连接MYSQL
mysql -u root -p  //新安装默认没有密码,输入后直接回车即可
或者
./mysql/bin/mysql -uroot

如果报错

[root@node1 support-files]# mysql -u root -p
Enter password:
ERROR  (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决办法

#修改/etc/my.cnf配置文件,增加配置
[client]
#password   = your_password
port        =
socket      = /tmp/mysql.sock

然后停止mysql服务并重启

解决

另外附注一份my.cnf的配置文件

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[client]
#password   = your_password
port        =
socket      = /var/lib/mysql/mysql.sock

[mysqld]
port        =
socket      = /var/lib/mysql/mysql.sock
datadir = /opt/module/mysql/data
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 1M
table_open_cache =
sort_buffer_size = 2M
net_buffer_length = 8K
read_buffer_size = 2M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 32M
thread_cache_size =
query_cache_size = 64M
tmp_table_size = 64M

explicit_defaults_for_timestamp = true
#skip-networking
max_connections =
max_connect_errors =
open_files_limit = 

log-bin=mysql-bin
binlog_format=mixed
server-id   =
expire_logs_days = 

#loose-innodb-trx=
#loose-innodb-locks=
#loose-innodb-
#loose-innodb-cmp=
#loose-innodb-cmp-per-index=
#loose-innodb-cmp-per-index-reset=
#loose-innodb-cmp-reset=
#loose-innodb-cmpmem=
#loose-innodb-cmpmem-reset=
#loose-innodb-buffer-page=
#loose-innodb-buffer-page-lru=
#loose-innodb-buffer-pool-stats=
#loose-innodb-metrics=
#loose-innodb-ft-
#loose-innodb-ft-inserted=
#loose-innodb-ft-deleted=
#loose-innodb-ft-being-deleted=
#loose-innodb-ft-config=
#loose-innodb-ft-index-cache=
#loose-innodb-ft-index-table=
#loose-innodb-sys-tables=
#loose-innodb-sys-tablestats=
#loose-innodb-sys-indexes=
#loose-innodb-sys-columns=
#loose-innodb-sys-fields=
#loose-innodb-sys-foreign=
#loose-innodb-sys-foreign-cols=

default_storage_engine = InnoDB
innodb_data_home_dir = /opt/module/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /opt/module/mysql/data
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit =
innodb_lock_wait_timeout = 

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 2M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

11.远程连接权限

(1)通过语句修改

use mysql;
#select 'host' from user where user='root';
update user set host = '%' where user ='root';
flush privileges; 

如果出现下图中的错误,直接忽略

【安装】Mysql在Linux上安装

(2)直接授权

GRANT ALL PRIVILEGES ON *.* TO ‘root’@'%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
13.sql基本语句
SHOW DATABASES;  //查看当前所有数据库
CREATE DATABASE 数据库名;  //创建数据库
USE  数据库名; //选择数据库
SOURCE SQL文件路径; //导入数据
GRANT ALL PRIVILEGES ON 数据库名.* TO 用户名@"%" IDENTIFIED BY '密码' WITH GRANT OPTION; //创建一个远程用户使他有某个数据库的所有权限
#启动mysql
service mysqld start
#关闭mysql
service mysqld stop
#查看运行状态
service mysqld status
**********************************************************************************************************************************************************
相关错误
1. ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
这是查看mysql status时报出的,删除对应的mysql文件即可
2.Starting MySQL.. ERROR! The server quit without updating PID file (/opt/module/mysql/data/node1.sunny.cn.pid).
 卸载重新安装,或者检查mysql目录的data/mysql文件夹权限是够有mysql权限
3.-bash: mysqld: command not found
原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令
 ln -s /usr/local/mysql/bin/mysql /usr/bin

然后再去/usr/bin目录下修改mysql命令为mysqld即可

mv mysql mysqld

4.-bash: mysqld_safe: command not found

该命令在mysql/bin目录下

 5.Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
做个软连接
 6.Access denied for user ''@'localhost' to database 'mysql'
 my.cnf配置文件中的mysqld中增加一行 skip-grant-tables,然后重启服务后
mysqld

mysql>   use  mysql ;
mysql>  update user set password =PASSWORD('NEW') where user='root';
mysql>   flush  privileges;   

my.cnf改回去之后重启mysql

ln -s /usr/local/mysql/bin/mysql /usr/bin