检测是否已经安装mysql
rpm -qa | grep mysql
[root@iZuf61hly6h2nsymj5xb5fZ local]# rpm -qa | grep mysql
mysql-libs-5.1.73-3.el6_5.x86_64
已经安装了,将其卸载
rpm -e --nodeps mysql-libs-5.1.73-3.el6_5.x86_64
下载mysql
进入安装包下载目录
cd /sharefree/environment
下载安装包(以5.6.34,通用版,64位linux为例):
wget http://101.96.10.73/dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.34-linux-glibc2.5-x86\_64.tar.gz
解压
tar -xzvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
复制解压后的mysql目录
cp -r mysql-5.6.34-linux-glibc2.5-x86_64 /usr/local/mysql
添加系统mysql组和mysql用户
添加用户组
/usr/sbin/groupadd mysql
添加用户mysql 到用户组mysql
/usr/sbin/useradd -g mysql mysql
安装mysql
进入安装mysql软件目录
cd /usr/local/mysql
修改当前目录拥有者为mysql用户
chown -R mysql:mysql ./
安装数据库
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
数据库安装完毕
添加mysql配置信息
添加配置信息
cp support-files/my-default.cnf /etc/my.cnf
修改配置文件my.cnf并保存(支持语言设置)
...
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
...
注意
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
必须放在mysqld节点下,否则client连接报错:
[root@iZuf61hly6h2nsymj5xb5fZ ~]# mysql -uroot -p
mysql: unknown variable 'sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'
一、登录MySQL查看用SHOW VARIABLES LIKE ‘character%'; 下字符集,显示如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
character_set_database和character_set_server的默认字符集还是latin1。
二、最简单的完美修改方法,修改mysql的my.cnf文件中的字符集键值(注意配置的字段细节):
1、在[client]字段里加入default-character-set=utf8,如下:
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
2、在[mysqld]字段里加入character-set-server=utf8,如下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
3、在[mysql]字段里加入default-character-set=utf8,如下:
[mysql]
no-auto-rehash
default-character-set=utf8
修改完成后,service mysql restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。
使用SHOW VARIABLES LIKE ‘character%';查看,发现数据库编码全已改成utf8。
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
将mysql服务加入开机自启动项
添加开机启动脚本
cp support-files/mysql.server /etc/init.d/mysql
通过chkconfig命令将mysql服务加入到自启动服务项中
chkconfig --add mysql
查看是否添加成功
chkconfig --list mysql
[root@iZuf61hly6h2nsymj5xb5fZ mysql]# chkconfig --list mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
重启系统,mysql就会自动启动
检查是否启动
netstat -anp|grep mysql
[root@iZuf61hly6h2nsymj5xb5fZ mysql]# netstat -anp|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2113/mysqld
tcp 0 0 172.19.192.131:3306 180.117.33.238:42286 ESTABLISHED 2113/mysqld
unix 2 [ ACC ] STREAM LISTENING 11448 2113/mysqld /tmp/mysql.sock
编辑软件运行环境
vim /etc/profile
vim i(编辑一些内容) esc(进入normal) w(保存文件) q(不保存退出文件)
###############################################################
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
###############################################################
使文件生效
source /etc/profile
启动mysql服务
手动启动mysql服务
service mysql start
修改数据库root用户密码
/usr/local/mysql/bin/mysqladmin -u root password '123456'
[root@iZuf61hly6h2nsymj5xb5fZ mysql]# /usr/local/mysql/bin/mysqladmin -u root password '123456'
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
解决方案
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p (命令未找到使用: ln -s /usr/local/mysql/bin/mysql /usr/bin)
Enter password: <输入新设的密码newpassword>
常用服务命令
启动mysql:service mysqld start
关闭mysql:service mysqld stop
重启mysql:service mysqld restart
查看运行状态:service mysqld status
开启远程访问
root登陆
mysql -uroot –p123456
设置远程访问(使用root密码):
grant all privileges on *.* to 'root' @'%' identified by '123456' with grant option;
flush privileges;
基础创建命令
创建数据库访问用户
create user 'suanmilk'@'%' identified by 'suanmilk';
create user 'suanmilk'@'localhost' identified by 'suanmilk'; -- 本地client使用
创建数据库
create database if not exists `sharefree`;
给用户分配访问密码
grant all privileges on sharefree.* to 'suanmilk'@'%' identified by 'suanmilk';
刷新
flush privileges;
卸载mysql
ps -ef | grep mysql
/etc/init.d/mysql status
whereis mysql
find / -name mysql【找到所有文件名为mysql的文件列表】
rm -rf /usr/local/mysql/【使用rm命令来移除列表中的文件】
rm -rf /etc/my.cnf【使用rm命令来移除列表中的文件】