一、Mac上的安装配置
// brew安装
brew install mysql
// 设置为开机启动
brew services start mysql
// 也可以手动自启
mysql.server start
// 登录
mysql -uroot
注意:
1)若登录时提示没有mysql这个命令,则需要将mysql的bin目录加入PATH:
export PATH=$PATH:/usr/local/mysql/bin
2)此时登录为默认状态,未设密码,若要设置密码,可在登录后运行如下命令:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your password');
二、Linux CentOS安装
在centos7上安装mysql5.6,采用source形式安装,过程如下:
1)安装依赖包:yum install -y gcc gcc-c++ kernel-devel ncurses-devel bison cmake
2)命令执行过程:
# Preconfiguration setup
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> mkdir build
shell> cd build
shell> cmake ..
shell> make
shell> make install
# End of source-build specific instructions
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql (这一步可能引发问题1)
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
3)环境变量配置
export PATH=$PATH:/usr/local/mysql/bin/
4)设置root用户密码
a、查看当前的用户及登录密码: SELECT User, Host, Password FROM mysql.user;
b、重置密码:
UPDATE mysql.user SET Password = PASSWORD('123456') where user='root';
FLUSH PRIVILEGES; // 刷新用户权限信息
c、退出重新登录: mysql -uroot -p
5)允许任何主机远程访问数据库
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
三、异常问题及解决办法
1、重新启动报错
=> 在my.cnf配置文件中[mysql]下加上tmpdir= /User/userName/mysql/data,或者删除/usr/local/mysql/data目录下logfile*文件
以下问题主要出现在CentOS7环境下的安装过程中:
2、FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:Data:Dumper
=> yum -y install autoconf
3、mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
=>
/etc/my.cnf
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
4、Can't connect to local MySQL server through socket '/tmp/mysql.sock'
(未解决,可能是之前卸载残留导致,重装了=_=!)
5、Ignoring query to other database
启动mysql时没有带-u参数
6、某个字断被截断,检查插入的数据类型是否与实际表结构中数据类型一致
7、 ERROR! MySQL server PID file could not be found!
Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/xiexin-01.pid).
=> 杀掉mysql进程,再重新启动
8、Table doesn’t exists error
=> The issue is that you need the ib* files in the root of the MySQL datadir (e.g. ibdata1, ib_logfile0 and ib_logfile1).
没有的话,要重建库了
9、utf8_general_ci incompatible with utf8mb4_unicode_ci
=> select * from messages where CONVERT(CIRCLE_ID USING utf8mb4) COLLATE utf8mb4_unicode_ci in(select CIRCLE_ID from circle_tenant where tenant_id='saq') and fresh_type='ORIGINAL'
10、导入数据时,被禁止
mysqlimport: Error: 1290, The MySQL server is running with the --secure-file-priv option so it cannot execute this statement, when using table: v_ssxx_jrxy
=> show variables like '%secure%’; // 查看当前变量值
重设:在my.cnf中mysql节下设置secure_file_priv=/Users/patty
或者启动时设置:mysql.server start —secure_file_priv=‘/Users/patty'
四、 相关命令
1、查看字符集
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
show full columns from users; // 产看表字段字符集
2、设置字符集,支持emoji表情符号
utf8mb4 可用于表情符号, 此时应设置数据库、表、表相关字段的字符集均为utf8mb4,方可实现数据的正常插入或是更新
set character_set_results=‘utf8mb4';
// 设置表字段字符集
alter TABLE articles MODIFY column title text CHARACTER set utf8mb4;
// 设置表字符集
ALTER TABLE articles CHARSET=utf8mb4;
ALTER TABLE T_CPDAILY_EMOTIONCOMMUNICATES CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3、字符串常用操作
1)替换 replace
UPDATE articles SET contUrl=REPLACE(contUrl, ‘/we', ‘/you'), sImgUrl=REPLACE(sImgUrl, '/root/asse', ''),
resUrl=REPLACE(resUrl, '/root/asse', '');
2)切割 split
// 设置split函数,返回指定子串长度
CREATE FUNCTION substrCount(x varchar(500), delim varchar(12)) returns int
return (length(x)-length(REPLACE(x, delim, '')))/length(delim);
select DISTINCT BM, BMFULL from v_ssxx_jrxy ORDER BY BM, substrCount(BMFULL,'/') desc;
3)substring_index内置函数,选择二级部门
SELECT DISTINCT BM, SUBSTRING_INDEX(BMFULL,'/',-1) as secondDepart from v_ssxx_jrxy ORDER BY BM
4、数据迁移
若输入mysqldump提示没有该命令,则启用命令:export PATH=$PATH:/usr/local/mysql/bin/mysqldump
导出整个数据库的表结构及数据:shell中运行>>> mysqldump -uuser -ppassword databaseName -h host > DB.sql
导入:mysqlClient中运行>>> source DB.sql
5、全文索引
innodb要求mysql 5.6.4以上版本支持
1)创建索引,可以在创建表时进行,也可以修改表或直接建立索引的方式
ALTER TABLE ARTICLES ADD COLUMN SEG_CONTENT VARCHAR(4000) DEFAULT NULL;
CREATE FULLTEXT INDEX IDX_SEG_CONTENT ON ARTICLES(SEG_CONTENT);
// 查询
SELECT * FROM ARTICLES WHERE MATCH(SEG_CONTENT) AGAINST(‘新鲜事’ IN BOOLEAN MODE);
MySQL目前支持全文搜索的还有:
NATURAL LANGUAGE MODEL: MyISAM引擎下,该模式对于词频超过50%的词视作停用词
2)操作过程中,可能会发现,对于中文经常会搜索不到结果,原因是,默认配置下innodb_ft_min_token_size的值为3,该值是针对英文
全文搜索的默认设置,用于过滤类似”a”,”to”这类停用词,对于中文,我们需要将该值设置为1 或者 2,否则大部分词长小于3的都将被过滤掉。
重启server设置token_size值为1: /etc/init.d/mysql.server restart --innodb_ft_min_token_size=1
SHOW VARIABLES LIKE 'innodb_ft_min_token_size'
删除原有的FullText Index:DROP INDEX IDX_FULL_TEXT_CONTENT ON ARTICLES;
重建全文索引:CREATE FULLTEXT INDEX IDX_CONTENT ON ARTICLES(content);
3) 设置本地停用词表
CREATE TABLE T_CPDAILY_STOPWORDS(value VARCHAR(30)) ENGINE = INNODB;
SET GLOBAL innodb_ft_server_stopword_table = 'cpdaily/T_CPDAILY_STOPWORDS';
4)设置配置项
show VARIABLES like 'innodb_%';
set GLOBAL innodb_optimize_fulltext_only=ON;
set global innodb_ft_aux_table = ‘cpdb/articles';
OPTIMIZE TABLE articles;
6、split
SELECT "信息工程系" as pd, SUBSTRING_INDEX(SUBSTRING_INDEX('信息工程系/计算机/通信','/',2),'/', -1) as sd, SUBSTRING_INDEX('信息工程系/计算机/通信','/',-1) as sec from DUAL五、卸载
Mac上的卸载
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
vim /etc/hostconfig (and removed the line MYSQLCOM=-YES-)
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
Linux上的卸载
1)
yum list installed mysql*
rpm -qa | grep -i mysql
2)
yum remove mysql mysql-devel mysql-server mysql-libs compat-mysql51
rpm -aq | grep -i mysql
3)
rm -rf /var/lib/mysql
4)
whereis mysql
rm -rf /usr/lib64/mysql
rm -rf /usr/local/mysql
rm -rf /usr/local/mysql/bin/mysql
5)
find / -name mysql
rm -rf /usr/lib/mysql
rm -rf /usr/share/mysql
rm -rf /usr/local/mysql-5.6.35/
rm -rf /run/mysql
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret
6)
chkconfig --list | grep -i mysql
chkconfig --del mysqld
rm -rf /var/log/mysql
注!!!
一、安装cmake:
// 获取安装包并解压缩
wget https://cmake.org/files/v3.3/cmake-3.3.2.tar.gz
tar xzvf cmake-3.3.2.tar.gz
// 进入到cmake文件夹中执行引导命令
cd cmake-3.3.2
./bootstrap
// 执行make
gmake
// 执行安装(root权限)
make install
二、安装boost
wget https://sourceforge.Net/projects/boost/files/boost/1.62.0/boost_1_62_0.tar.gz
tar -zxvf boost_1_62_0.tar.gz
cp -r boost_1_62_0 /usr/local/boost
三、利用ibd文件恢复数据库
1.在新库里面建一个表 表结构需要和要恢复的表结构一致
CREATE TABLE `xxx` (
`id` int(32) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 移除表空间ALTER TABLE xxx DISCARD TABLESPACE;
3. 将ibd文件移至数据库文件下,我这里路径是/var/lib/mysql/test2/。
4. 将ibd文件权限放开 chmod 777 /var/lib/mysql/test2/xxx.ibd
5. 导入表空间 ALTER TABLE xxx IMPORT TABLESPACE;