下载
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
安装
- 解压
tar -xvzf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
- 重命名文件夹,把解压后的文件COPY到/usr/local/mysql目录
cp -r mysql-5.6.33-linux-glibc2.5-x86_64 /usr/local/mysql
- 添加mysql用户组以及用户
groupadd mysql
useradd -r -g mysql mysql
- 创建mysql数据目录,新目录不存在则创建
数据库数据默认目录datadir=/var/lib/mysql,可通过vim /etc/my.cnf 查看
cd /usr/local/mysql/
mkdir -p ./data/mysql
- 修改目录权限
chown -R mysql:mysql ./
- 初始化数据库
./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql
安装时如果遇到以下上错误,则需要先安装另一个软件
yum -y install autoconf
- 安装完成添加启动服务,并授权
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
- 初始化启动脚本
cp support-files/my-default.cnf /etc/my.cnf
- 配置启动脚本
vi /etc/init.d/mysqld
修改以下配置项
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/mysql
- 现在可以启动服务
service mysqld start
- 测试连接
./mysql/bin/mysql -uroot
- 加入环境变量,可以让mysql在任何地方运行
export PATH=$PATH:/usr/local/mysql//bin
source /etc/profile
至此,mysql已运行起来了,接下来做些配置
-
查看端口号
登陆mysql,执行以下命令,默认端口为3306
修改端口号
编辑/etc/my.cnf文件,增加端口参数
vim /etc/my.cnf
[mysqld]
port=3506
- 重启mysql
service mysqld restart
-
再次查询mysql 端口,端口已发生改变
配置ECS安全规则
虽然按上面步骤已安装就启动了mysql,但需要开放出端口才可以远程访问,增加以下规则
允许 自定义 TCP 13306/13306 地址段访问 XX.XX.XX.0/24
现在通过客户端去访问mysql,会提示10038错误,如下图
需要开放远程登陆权限,解决方案如下
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
新建用户以及数据库并授权
一般情况下,root用户不对外开放,所以需要新建用户以及数据库并进行授权
- root登陆mysql创建数据库
create database nacos default character set utf8mb4 collate utf8mb4_unicode_ci;
- 授权db_egold数据库给新用户
grant all privileges on `db_egold`.* to 'egoldtest'@'%' identified by 'yourpasswd';
flush privileges;
查看用户,新建的用户已创建,即可以通过远程登陆访问
其它问题
- 大小写
遇到一个问题,总提示某个表不存在,但这个表明明是存在于数据库中的,后面查才发现默认是区分大小写的,我们需要改成不区分大小写
vim /etc/my.cnf
在[mysqld]下加入一行:
lower_case_table_names=1
然后重启,即可解决
- 修改密码,忘记密码的事情经常发布
set password for username@localhost = password('密码');
-字符集问题
学会查看默认字符集
show variables like'%char%';
设置默认字符集,需要在/etc/my.cnf的[mysqd]下面增加
character_set_server=utf8
character_set_server = utf8
修改已有数据库字符集
最简单可以通用Navicat for mysql直接修改
- 忘记密码
1.修改MySQL的登录设置:
vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
保存并且退出
2、重新启动mysql
service mysqld restart
3、登录并修改登录密码
#mysql
#use mysql
update user set Password=password('密码') where user='root' ;
quit;
4、强配置改回来
去掉 skip-grant-tables
5、重新启动mysql
service mysqld restart
1. 打开一个终端窗口
2. 输入 sudo /usr/local/mysql/support-files/mysql.server stop //也可以在活动监视器里面进程里面杀掉mysqld
3. 输入 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
4. 这时不要关窗口,再打开一个终端窗口
5. 输入 sudo /usr/local/mysql/bin/mysql -u root
6. 这时候会出现mysql>了,输入use mysql
7. 最后输入 update user set authentication_string=password('你需要设置的密码') where user='root';
到此结束,可以重启下mysql server
8. SET PASSWORD = PASSWORD('newpassward');
6、设置默认字符集
[client]
default_character_set=utf8mb4
[mysqld]
port=13306
# 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 = .....
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/mysql
character_set_server=utf8mb4
collation_server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip_character_set_client_handshake=true
lower_case_table_names=1
# 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
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysql]
default_character_set=utf8mb4
- 生成删除语句
SELECT distinct CONCAT('delete from ',table_name, ' where 1=1;') FROM information_schema.tables WHERE table_type='base table' and TABLE_NAME LIKE 'shop_%';
备份数据库
mysqldump -hlocalhost -P3306 -uroot -p123456 db_cinema> db_cinema_20200923.sql;
安装后,root密码是空的,需要初始化
···
update user set password=PASSWORD('fads') where User='root';
···
key过长
set global innodb_large_prefix=on;
set global innodb_file_per_table=on;
set global innodb_file_format=BARRACUDA;
set global innodb_file_format_max=BARRACUDA;
启动失败时查看日志
/usr/local/mysql/data/mysql/xxx.err