linux中mysql数据库安装

1.卸载旧版本

使用下面命令检查是否安装mysql
bash > rpm -q mysql;
有的话通过下面命令进行删除
bash > rpm -e --nodeps mysql // 强制删除
bash > rpm -e mysql //普通删除

2.安装mysql

安装编译代码需要的包

yum -y install make gcc-c++ cmake bison-devel  ncurses-devel

下载mysql数据库 mysql-5.6.14.tar.gz并上传到/opt/
解压

tar -zxvf  mysql-5.6.14.tar.gz

然后进到mysql-5.6.14

cd  mysql-5.6.14

编译安装源码

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/data/mysqldb \
-DMYSQL_TCP_PORT=3306 \
-DENABLE_DOWNLOADS=1

编译并安装

make && make install

3.配置mysql

设置权限
查看是否有mysql用户和组

cat  /etc/passwd
cat  /etc/group

如果没有的话创建

groupadd mysql
useradd -g mysql  mysql

修改/usr/local/mysql权限

chown  -R  mysql:mysql  /usr/local/mysql

初始化配置,进入安装路径再执行下面的指令,执行初始化配置脚本,创建自带的数据库和表

cd  /usr/local/mysql

scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid --tmpdir=/tmp

注:在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf",在本例中就是 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!
在CentOS 6.8版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
修改名称,防止干扰:
mv /etc/my.cnf /etc/my.cnf.bak
修改/usr/local/mysql/my.cnf
vim /usr/local/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

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
basedir = /usr/local/mysql
datadir = /data/mysql
#socket = /tmp/mysql.sock
socket = /usr/local/mysql/mysql.sock
log-error = /data/mysql/error.log
pid-file = /data/mysql/mysql.pid
user = mysql
tmpdir = /tmp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 


auto_increment_increment=2
auto_increment_offset=1
#replicate-do-db=test
replicate-ignore-db=information_schema
skip_name_resolve     = 1
skip_external_locking = 1
max_allowed_packet    = 32M
max_connections       = 1000
wait_timeout          = 500
interactive_timeout   = 300
max_connect_errors    = 100
back_log              = 600
performance_schema    = 1
table_open_cache      = 2048
lower_case_table_names = 1


read_rnd_buffer_size = 256K
read_buffer_size     = 256K
join_buffer_size     = 256K
sort_buffer_size     = 256K
thread_stack         = 256K
tmp_table_size       = 32M
max_heap_table_size  = 32M
#
##global_buffers
innodb_buffer_pool_size         = 5G
innodb_additional_mem_pool_size = 16M
innodb_log_buffer_size          = 16M
innodb_flush_log_at_trx_commit  = 2
key_buffer_size                 = 64M

创建/data/mysql

mkdir -p  /data/mysql

启动mysql
添加服务,拷贝服务脚本到init.d目录,并设置开机启动

cp  /usr/local/mysql/support-files/mysql.server   /etc/init.d/mysql
chkconfig  mysql  on
service mysql start

执行下面命令修改root密码

cd   /usr/local/mysql/bin
./mysql  -u root  -p
mysql> SET PASSWORD = PASSWORD('root')

4.远程连接mysql数据库

1)防火墙放开mysql数据库的3306端口



2)默认mysql数据库不允许远程连接,需要修改配置
在linux上登录mysql数据库

cd  /usr/local/mysql/bin
./mysql  -uroot -p

3)切换到mysql表空间

use  mysql;

4)执行下面指令
all :表示将所有权限授予给用户。也可指定具体的权限,如select、create、drop等。
on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名;
to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示yangxin这个用户只能在192.168.0IP段登录
identified by:指定用户的登录密码
with grant option:表示允许用户将自己的权限授权给其它用户

grant all on *.*   to root@'%' identified by 'root';
flush privileges;

5.无密码登录并修改密码

1)第一步:停止mysql服务

service mysql stop

2)第二步:跳过密码验证

/usr/local/mysql/bin/mysqld_safe --skip-grant-tables

3)第三步:另开一个窗口 注意

/usr/local/mysql/bin/mysql -uroot -p

切换到mysql表空间

mysql > use mysql

修改密码

mysql > update user set password=password('root') where user="root";

你可能感兴趣的:(linux中mysql数据库安装)